Skip to content

Latest commit

 

History

History
201 lines (128 loc) · 8.39 KB

File metadata and controls

201 lines (128 loc) · 8.39 KB

Dealing with files:

Opening a File: The open() Function

file = open("text.txt")

File Modes (The 'How-To'):

The most common modes you'll use are:

Mode Meaning What it Does
"r" Read Opens the file only for reading. If the file doesn't exist, Python will give you an error.
"w" Write Opens the file for writing. CAUTION: If the file already exists, it will delete all its content and start fresh! If the file doesn't exist, Python will create a new one.
"a" Append Opens the file for writing, but adds new content to the end of whatever is already in the file. It's like flipping to the last blank page of the notebook. If the file doesn't exist, Python will create a new one.

Reading and Writing a File:

file = open("text.txt")
content = file.read()
print(content)
Action Python Method Description
Read Everything file_handle.read() Reads the entire contents of the file as one big string.
Read Line by Line file_handle.readlines() Reads all lines and returns them as a list of strings, where each string is one line from the file.
Write Data file_handle.write(data) Writes the string contained in the data variable to the file.

Closing a File:

file = open("text.txt")
content = file.read()
print(content)
file.close()

Why is closing so important?

Action Description
Saving Data 💾 When you write to a file, Python sometimes holds the data temporarily (buffering) until it has enough to write efficiently. close() forces Python to take any unsaved changes and actually write them to the file on your disk.
Freeing Resources 🔓 It tells your computer, "I'm done with this file," which frees up the file so other programs (or other parts of your own program) can open and use it.

The Better Way: The with Statement

Because closing a file is so essential and easy to forget, Python has a much safer and cleaner way to handle files called the with statement.

with open("text.txt") as file:
    content = file.read()
    print(content)

# You don't need to call .close()

Writing to a File:

The write() method saves the content you give it as a string to the file and returns an integer to your Python program.
It doesn't matter what you put isnide the write() method, It should be saved as a string in the file, even if it's a number.

Example:

We have a highscore.txt file and the content is 0.

  • To read the content:

    with open("highscore.txt") as file:
      content = file.read()
      print(content)
  • To change the data from this file, let's say 55 for example:

    with open("highscore.txt", "w") as file:
      content = file.write("55")
      print(content)

As you can see we used string for the write() method : file.write("55")
If you write your data for the write() method with the string like : file.write(55), you'll run into an error: TypeError: write() argument must be str, not int`

Note:
The default mode is set toread-only ("r")

Mode Meaning What it Does
"r" Read Opens the file only for reading. If the file doesn't exist, Python will give you an error.
"w" Write Opens the file for writing. CAUTION: If the file already exists, it will delete all its content and start fresh! If the file doesn't exist, Python will create a new one.
"a" Append Opens the file for writing, but adds new content to the end of whatever is already in the file. It's like flipping to the last blank page of the notebook. If the file doesn't exist, Python will create a new one.
with open("text.txt", "a") as file:
    content = file.write("\nHiiiii")
    print(content)

Note:
When you try to open a file in a write mode and that file doens't exist, then it's going to actually creat it fro you from scratch.


Mixing Classes and Managing Files

  1. Read and Convert Separately:

        def __init__(self):
         with open("highscore.txt") as file:
             highscore = file.read()
         self.high_score = int(highscore)
  2. Read and Convert Inline:

        def __init__(self):
         with open("highscore.txt") as file:
             self.high_score = int(file.read())

💡 The Difference and Which is Better

Feature Read and Convert Separately (Your first option) Read and Convert Inline (My suggestion)
Code highscore = file.read() followed by self.high_score = int(highscore) self.high_score = int(file.read())
Readability Slightly higher. Breaking it into two steps (read, then convert) is often clearer for beginners to trace. Slightly lower. It's a bit more compact but requires understanding that int() is working on the result of file.read().
Efficiency Technically the same. The Python interpreter handles them nearly identically. Technically the same.
Better for You Recommended. Since you are a beginner, this approach is easier to follow and helps you clearly see the two necessary steps: reading the string and then converting it to an integer. This is the "Pythonic" way once you are more comfortable, as it's more concise.

integer into a string in file.write()

Method Code How it Works
Your Method file.write(str(self.highscore)) Explicitly calls the built-in str() function to convert the integer to a string.
Course Method file.write(f"{self.highscore}") Uses an f-string (Formatted String Literal). When you place a variable inside the curly braces {} within an f-string, Python automatically converts that variable to its string representation.

Which is Best Practice?

In modern Python, the course's method (using f-strings) is generally considered best practice for converting non-string values into strings, especially when dealing with output or file writing.

Here's why:

  • Readability: It's very clear what the purpose is: embedding a variable directly into a string.
  • Conciseness: It's often the most concise way to perform the conversion.
  • Versatility: While you only have one variable here, f-strings are designed to handle multiple variables and pieces of text at once, making them highly versatile for complex output strings.

Summary

Method Pros Cons Best for...
str() function Clear, explicit, good for beginners. A little more verbose (more characters to type). When you are performing a complex, single conversion.
f-string (f"{}") Concise, modern, highly readable (Pythonic). Requires placing the variable inside curly braces {}. General file writing and string formatting.

Read Text All at Once vs Line-by-Line (The Loop Approach) :

Read Text All at Once:

For tasks like lowercasing, removing punctuation, and fixing whitespace, it's far easier and more efficient to have the entire text as a single, massive Python string.

  • Simple Operations
  • Context for Whitespace

Line-by-Line (The "Loop" Approach):

Using a nested loop to go through lines and then words is usually better for tasks that involve memory management or record processing:

Feature Read All at Once (The Best Choice) Line-by-Line (The Loop Approach)
Punctuation You use one string method to clean the entire text in one go. You have to clean each individual line, which is slower and more complicated.
Lowercasing One simple method (.lower()) on the whole string. You have to call the method inside a loop, building a new structure line by line.
Whitespace The Split and Join logic (our Step 4) works perfectly across the entire document, even if the excess spaces were originally line breaks. It becomes very tricky to handle extra spaces or line breaks that occur between the end of one line and the beginning of the next.
Simplicity Leads to a much shorter, cleaner, and easier-to-read script. Requires more code to stitch the text back together after processing.