Handling Files in Python
You’ll learn how to read from and write to files. You open a file, read or write its contents, and close it when you’re done.
Opening a file
To work with a file, you open it with the open() function. You pass the filename (and optionally the path) and a mode: read, write, or append.
my_file = open("example.txt", "r")Here we open example.txt in read mode and store the result in a variable. You’ll use that variable to read from or write to the file.
Modes:
"r"(read) — Open for reading. The file must already exist. You can’t write in this mode."w"(write) — Open for writing. If the file exists, its contents are replaced. If it doesn’t exist, a new file is created."a"(append) — Open for adding to the end. New content is written after what’s already there. Creates the file if it doesn’t exist.
Reading from a file
After opening in read mode, you can get the contents in a few ways.
Loop over the file line by line — The simplest way to read: loop directly over the file. Each time through the loop you get the next line (as a string). Use this when you want to process one line at a time (e.g. a list of numbers or names, one per line).
my_file = open("example.txt", "r")
for line in my_file:
print(line.strip())
my_file.close()read() — Reads the entire file as one string.
content = my_file.read()
print(content)readline() — Reads one line at a time. Each call gives you the next line.
line = my_file.readline()
print(line)readlines() — Reads all lines and returns them as a list of strings. You can then loop over that list.
lines = my_file.readlines()
for line in lines:
print(line)When you read a line, it usually includes the newline at the end. Use .strip() to remove it if you don’t want it (e.g. line.strip() before converting to a number).
Writing to a file
Open the file in write ("w") or append ("a") mode. Use the write() method to add text. write() doesn’t add a newline for you—add \n at the end of a string if you want a new line.
my_file = open("example.txt", "w")
my_file.write("Hello, World!\n")
my_file.write("This is a new line.\n")
my_file.close()That creates or overwrites example.txt with two lines. To add more lines without wiping the file, open in append mode:
my_file = open("example.txt", "a")
my_file.write("This line is appended.\n")
my_file.close()Now the file has the two original lines plus the new one at the end.
Closing a file
When you’re done, close the file with close(). That frees resources and makes sure everything is written to disk. Think of it like saving and closing a document.
my_file.close()If you forget to close, Python may close it when the program ends, but it’s good practice to close when you’re finished.
Using with (optional)
A common pattern is to use with so the file is closed automatically when the block ends—even if something goes wrong. You don’t have to call close() yourself.
with open("example.txt", "r") as my_file:
content = my_file.read()
print(content)Once the block under with finishes, the file is closed. You can use this for reading or writing. If you’re comfortable with open/close, you can try with next.
Summary
- Open a file with
open(filename, mode), then read (e.g. loop over the file, or useread()/readlines()) or write (usewrite()). - Close the file with
close()when you’re done, or use awithblock so it closes automatically. - When your file has one value per line (e.g. one grade per line), open it and loop with
for line in file:.