Working with Files in Python
Reading from and writing to files is one of the most common tasks in programming. In Python, this is incredibly simple, but a small mistake—like forgetting to close a file—can waste system resources or corrupt your data.
Why Use a Context Manager (the with statement)?
When you open a file using the open function, you must eventually close it. If an error occurs in the middle of your program, the line that closes the file might never run. The with statement guarantees that the file is closed automatically and safely as soon as the block of code finishes executing—even if an error occurs.
Reading and Writing Files in Python
To write to a new file:
with open("note.txt", "w", encoding="utf-8") as file:
file.write("Hello Python!")
To read data from the same file:
with open("note.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)
Important Tips
-
The
"w"mode overwrites the existing file. To append text to the end of a file without deleting its contents, use the"a"(append) mode. -
Always specify
encoding="utf-8"to prevent text encoding issues when your code is run on different operating systems. -
For large files, avoid loading the entire content into memory using
read(). Instead, iterate through the file line-by-line using a for loop to save RAM.