File Handling in Python
Published by Brayan K
Learn to read, write, and manage files to save data permanently.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
open() gives a file object, and with closes it
open() takes the file's name, a mode ("w" to write, nothing or "r" to read) and an encoding, and hands back a file object, not the text. The text comes from the object's methods, such as read(). A with block closes the file when the block ends, as Working with JSON and Reading CSV & Excel Files did without explaining:
with open("todo.txt", "w", encoding="utf-8") as f:
f.write("Buy milk\n")
f.write("Walk the dog\n")
with open("todo.txt", encoding="utf-8") as f:
print(f)
text = f.read()
print(repr(text))
print(f.closed)
# ✅ Expected output:
# <_io.TextIOWrapper name='todo.txt' mode='r' encoding='utf-8'>
# 'Buy milk\nWalk the dog\n'
# Trueprint(f) shows the object: a TextIOWrapper, Python's name for a text file, with its name, mode and encoding. The text keeps its line breaks, and once the block has ended, f.closed is True. So a loop that has slipped out of the block can't read:
with open("todo.txt", "w", encoding="utf-8") as f:
f.write("Buy milk\nWalk the dog\n")
with open("todo.txt", encoding="utf-8") as f:
heading = f.readline()
for line in f:
print(line)
# ✅ Expected error:
# ValueError: I/O operation on closed file.Without with, the file stays open until close() is called, and until then what you wrote may not be in the file yet:
from pathlib import Path
f = open("notes.txt", "w", encoding="utf-8")
f.write("Buy milk\n")
print(repr(Path("notes.txt").read_text(encoding="utf-8")))
f.close()
print(repr(Path("notes.txt").read_text(encoding="utf-8")))
# ✅ Expected output:
# ''
# 'Buy milk\n'Python keeps written text in memory and saves it in batches; closing the file saves the rest. A with block closes the file even when an error stops the program inside it. Context Managers & the with Statement in Depth shows how with does that. A file name on its own means a file in the working folder; Modern File Paths with pathlib covers paths and folders, and open() takes a Path as well as text.
"w" empties the file, "a" adds to it, "x" refuses
"w" creates the file, or empties it if it's there, as soon as it opens. "a" appends: it adds to the end and keeps what's there, which is how a log grows without being rewritten:
with open("log.txt", "w", encoding="utf-8") as f:
f.write("09:00 start\n")
with open("log.txt", "a", encoding="utf-8") as f:
f.write("09:05 login\n")
with open("log.txt", encoding="utf-8") as f:
print(f.read().splitlines())
with open("log.txt", "w", encoding="utf-8") as f:
pass
with open("log.txt", encoding="utf-8") as f:
print(repr(f.read()))
# ✅ Expected output:
# ['09:00 start', '09:05 login']
# ''The second "w" wrote nothing at all, and the log is empty: opening was enough. Appending also trusts that the file ends with a line break. If it doesn't, the new text carries on from the last line:
with open("scores.txt", "w", encoding="utf-8") as f:
f.write("Ada 10\nBob 8")
with open("scores.txt", "a", encoding="utf-8") as f:
f.write("Cara 12\n")
with open("scores.txt", encoding="utf-8") as f:
print(f.read().splitlines())
# ✅ Expected output:
# ['Ada 10', 'Bob 8Cara 12']A mode is a set of letters with one job each, so a made-up mix such as "rw" stops before any file is touched:
with open("log.txt", "rw", encoding="utf-8") as f:
print(f.read())
# ✅ Expected error:
# ValueError: must have exactly one of create/read/write/append mode"x" creates a file only if there's none with that name, so a script can't wipe a report it made before. Opening an existing file with "x" stops with FileExistsError; its message carries an error number in square brackets that's different in this site's editor, so it isn't shown as a run here. This example deletes its file at the end, so it works every time it runs:
from pathlib import Path
with open("report.txt", "x", encoding="utf-8") as f:
f.write("first run\n")
print(Path("report.txt").read_text(encoding="utf-8"), end="")
Path("report.txt").unlink()
# ✅ Expected output:
# first runwrite() takes one string and adds nothing
write() writes exactly the text it's given, with no line break of its own, and returns how many characters it wrote. print() with file= writes into a file the way it writes to the screen: it turns values into text, puts spaces between them and ends the line:
with open("scores.txt", "w", encoding="utf-8") as f:
n = f.write("Ada 10\n")
print("Bob", 8, file=f)
print("Cara", 12, sep=",", file=f)
print(n)
with open("scores.txt", encoding="utf-8") as f:
print(f.read().splitlines())
# ✅ Expected output:
# 7
# ['Ada 10', 'Bob 8', 'Cara,12']write() doesn't convert anything, so a number has to become text first, with str() or an f-string:
score = 10
with open("scores.txt", "w", encoding="utf-8") as f:
f.write(score)
# ✅ Expected error:
# TypeError: write() argument must be str, not intTwo arguments are a mistake too: f.write("Ada", "10") stops with TypeError: TextIOWrapper.write() takes exactly one argument (2 given). And file= wants the file object, not its name:
print("Ada 10", file="scores.txt")
# ✅ Expected error:
# AttributeError: 'str' object has no attribute 'write'writelines() writes each string in a list one after another. Despite its name, it doesn't add line breaks either; a generator expression, from Comprehensions, adds them:
names = ["Ada", "Bob", "Cara"]
with open("names.txt", "w", encoding="utf-8") as f:
f.writelines(names)
with open("names.txt", encoding="utf-8") as f:
print(repr(f.read()))
with open("names.txt", "w", encoding="utf-8") as f:
f.writelines(name + "\n" for name in names)
with open("names.txt", encoding="utf-8") as f:
print(repr(f.read()))
# ✅ Expected output:
# 'AdaBobCara'
# 'Ada\nBob\nCara\n'For CSV and JSON, open the file this way and hand it to the csv module, as Reading CSV & Excel Files does, or to json.dump() and json.load(), as Working with JSON does.
A file object is used up after one read
A file object remembers how far it has read. After read() it's at the end, so a second read() gets an empty string and readlines() an empty list. seek(0) goes back to the start:
with open("todo.txt", "w", encoding="utf-8") as f:
f.write("Buy milk\nWalk the dog\n")
with open("todo.txt", encoding="utf-8") as f:
first = f.read()
second = f.read()
print(len(first), repr(second))
print(f.readlines())
f.seek(0)
print(f.readlines())
# ✅ Expected output:
# 22 ''
# []
# ['Buy milk\n', 'Walk the dog\n']A for loop over the file reads one line at a time, and a second loop over the same object finds nothing left, like the zip objects in enumerate & zip:
with open("todo.txt", "w", encoding="utf-8") as f:
f.write("Buy milk\nWalk the dog\n")
with open("todo.txt", encoding="utf-8") as f:
total = 0
for line in f:
total += 1
again = 0
for line in f:
again += 1
print(total, again)
# ✅ Expected output:
# 2 0Open the file again, or seek(0), to read it twice. Jumping around inside big files is in Working with Files, Streams & Large Datasets.
Every line keeps its \n, except maybe the last
readline() reads one line, line break included, and gives '' at the end of the file. readlines() gives all of them as a list, each with its \n. The last line has one only if the file ends with a line break:
with open("todo.txt", "w", encoding="utf-8") as f:
f.write("Buy milk\nWalk the dog")
with open("todo.txt", encoding="utf-8") as f:
print(repr(f.readline()))
print(repr(f.readline()))
print(repr(f.readline()))
with open("todo.txt", encoding="utf-8") as f:
print(f.readlines())
# ✅ Expected output:
# 'Buy milk\n'
# 'Walk the dog'
# ''
# ['Buy milk\n', 'Walk the dog']That \n is why printing each line of a file leaves a blank line after it, and why a line never equals the text you see:
with open("todo.txt", "w", encoding="utf-8") as f:
f.write("Buy milk\nWalk the dog\n")
with open("todo.txt", encoding="utf-8") as f:
for line in f:
print(line)
print("---")
with open("todo.txt", encoding="utf-8") as f:
for line in f:
print(line.rstrip("\n"), line == "Buy milk", line.rstrip("\n") == "Buy milk")
# ✅ Expected output:
# Buy milk
#
# Walk the dog
#
# ---
# Buy milk False True
# Walk the dog False Falserstrip("\n") takes off just the line break; strip() would take spaces at the start of the line too. int() and float() ignore the line break around a number, but a blank line is nothing but a line break:
with open("counts.txt", "w", encoding="utf-8") as f:
f.write("42\n\n")
with open("counts.txt", encoding="utf-8") as f:
for line in f:
print(int(line) + 1)
# ✅ Expected output:
# 43
# ✅ Expected error:
# ValueError: invalid literal for int() with base 10: '\n'Skip the lines that are empty once stripped:
with open("prices.txt", "w", encoding="utf-8") as f:
f.write("4.50\n2.25\n\n3.00\n")
total = 0
with open("prices.txt", encoding="utf-8") as f:
for line in f:
if line.strip():
total += float(line)
print(total)
# ✅ Expected output:
# 9.75Text, bytes and the encoding
A file stores bytes, and the encoding says how characters become bytes. Without encoding=, Python uses the computer's default, which isn't the same everywhere, so every example here passes encoding="utf-8", as Modern File Paths with pathlib does. Adding b to the mode, as in "rb", reads the bytes themselves, shown as a bytes value such as b'caf\xc3\xa9\n', where each \x and two hex digits is one byte:
with open("menu.txt", "w", encoding="utf-8", newline="\n") as f:
print(f.write("café\n"))
with open("menu.txt", "rb") as f:
data = f.read()
print(data, len(data))
with open("menu.txt", encoding="latin-1") as f:
print(f.read().strip())
# ✅ Expected output:
# 5
# b'caf\xc3\xa9\n' 6
# cafénewline="\n" keeps the written line ending as one LF byte on every platform. write() counted 5 characters, but the file holds 6 bytes: in UTF-8, é takes two. Read with the wrong encoding, those two bytes turn into two other characters, with no error. pathlib's lesson shows the other outcome, a UnicodeDecodeError. Binary mode, "rb" and "wb", is for files that aren't text, such as images, and it takes bytes, not strings:
with open("menu.bin", "wb") as f:
f.write("café\n")
# ✅ Expected error:
# TypeError: a bytes-like object is required, not 'str'For the same reason, open("menu.txt", "rb", encoding="utf-8") stops with ValueError: binary mode doesn't take an encoding argument. Pass the encoding by name for clarity: the third positional argument to open() is a number called buffering, and encoding is fourth. Putting it third fails:
with open("menu.txt", "w", encoding="utf-8") as f:
f.write("café\n")
with open("menu.txt", "r", "utf-8") as f:
print(f.read())
# ✅ Expected error:
# TypeError: 'str' object cannot be interpreted as an integerStrings, Bytes & Unicode Encodings explains bytes and encodings in depth.
Text mode translates line ends
Windows ends lines with two characters, \r\n; Linux and macOS use \n. In text mode Python reads either as \n, and on Windows it writes \n as \r\n. newline="\r\n" asks for Windows line ends anywhere, and newline="" turns the translation off when reading:
with open("win.txt", "w", encoding="utf-8", newline="\r\n") as f:
f.write("Ada,10\nBob,8\n")
with open("win.txt", encoding="utf-8") as f:
print(repr(f.read()))
with open("win.txt", encoding="utf-8", newline="") as f:
print(repr(f.read()))
# ✅ Expected output:
# 'Ada,10\nBob,8\n'
# 'Ada,10\r\nBob,8\r\n'This translation is why Reading CSV & Excel Files opens every CSV file with newline="": the csv module writes its own line ends.
A file that isn't there
Reading a file that doesn't exist stops with FileNotFoundError, with the message No such file or directory: 'notes.txt'. "w" and "a" stop the same way when the folder in the path doesn't exist. These messages carry an error number in square brackets that's different in this site's editor, so they aren't shown as runs here. Exception Handling shows how to catch the error and carry on. Until then, check first with is_file(), or use "a", which creates the file when it's missing:
from pathlib import Path
notes = Path("notes.txt")
print(notes.is_file())
with open(notes, "a", encoding="utf-8") as f:
f.write("first note\n")
print(notes.is_file())
with open(notes, encoding="utf-8") as f:
print(f.read(), end="")
notes.unlink()
# ✅ Expected output:
# False
# True
# first noteWorked example: reading a log file
Regular Expressions (re) parsed a log held in a string. Here it's a real file: written with "w", one line added with "a" and print(file=), then read a line at a time with enumerate() numbering the lines, and a summary saved to a second file:
import re
with open("app.log", "w", encoding="utf-8") as f:
f.write("2024-01-15 09:12:03 ERROR db connection refused\n")
f.write("2024-01-15 09:12:44 WARN api slow response 1420ms\n")
f.write("2024-01-15 09:13:01 ERROR api timeout after 30s\n")
with open("app.log", "a", encoding="utf-8") as f:
print("2024-01-15 09:15:22 INFO web deploy finished", file=f)
LINE = re.compile(r"(\S+) (\S+) (\w+)\s+(\w+)\s+(.+)")
errors = 0
with open("app.log", encoding="utf-8") as f:
for number, line in enumerate(f, start=1):
m = LINE.match(line)
if m and m.group(3) == "ERROR":
errors += 1
print(f"line {number}: {m.group(2)} {m.group(4):<4}{m.group(5)}")
print("Errors found:", errors)
with open("errors.txt", "w", encoding="utf-8") as out:
print("errors:", errors, file=out)
with open("errors.txt", encoding="utf-8") as f:
print(repr(f.read()))
# ✅ Expected output:
# line 1: 09:12:03 db connection refused
# line 3: 09:13:01 api timeout after 30s
# Errors found: 2
# 'errors: 2\n'Each line still ends in \n, but the last group needs no strip(): in a pattern, . matches any character except a line break, so the match stops before it.
Practice: a scores file
Write two scores to a new file, add a third without losing them, then read the file back and total the points. The first two blanks are modes and the third splits a line into its two parts; the hints at the end name them.
with open("scores.txt", ___, encoding="utf-8") as f:
f.write("Ada 10\n")
f.write("Bob 8\n")
with open("scores.txt", ___, encoding="utf-8") as f:
print("Cara", 12, file=f)
total = 0
with open("scores.txt", encoding="utf-8") as f:
for line in f:
name, points = line.___()
total += int(points)
print(name, points)
print("Total:", total)
# 👉 replace ___ with "w"
# 👉 replace ___ with "a"
# 👉 replace ___ with split
# ✅ Expected output:
# Ada 10
# Bob 8
# Cara 12
# Total: 30On your own: visits per page
visits.log has one visit per line: a date and a page. Count the visits per page, write report.txt with one line per page, busiest first and ties in alphabetical order, then add a total line with "a", and print the report. The starter writes the log.
with open("visits.log", "w", encoding="utf-8") as f:
f.write("2024-06-01 /home\n")
f.write("2024-06-01 /pricing\n")
f.write("2024-06-02 /home\n")
f.write("2024-06-02 /blog\n")
f.write("2024-06-03 /home\n")
f.write("2024-06-03 /pricing\n")
# your code here: count, write report.txt, add the total, print itwith open("visits.log", "w", encoding="utf-8") as f:
f.write("2024-06-01 /home\n")
f.write("2024-06-01 /pricing\n")
f.write("2024-06-02 /home\n")
f.write("2024-06-02 /blog\n")
f.write("2024-06-03 /home\n")
f.write("2024-06-03 /pricing\n")
counts = {}
with open("visits.log", encoding="utf-8") as f:
for line in f:
day, page = line.split()
counts[page] = counts.get(page, 0) + 1
def busiest(pair):
return (-pair[1], pair[0])
with open("report.txt", "w", encoding="utf-8") as report:
for page, visits in sorted(counts.items(), key=busiest):
print(f"{page:<10}{visits:>3}", file=report)
with open("report.txt", "a", encoding="utf-8") as report:
print(f"{'total':<10}{sum(counts.values()):>3}", file=report)
with open("report.txt", encoding="utf-8") as report:
print(report.read(), end="")
# ✅ Expected output:
# /home 3
# /pricing 2
# /blog 1
# total 6The sort key returns a tuple, compared item by item as in Tuples: most visits first (the minus sign), then the page name.
Practice quiz
todo.txt holds two lines. Then: with open("todo.txt", "w", encoding="utf-8") as f: pass. What does reading todo.txt give now?
- The two lines
- ''
- ' '
- It stops with an error
Answer: ''. "w" empties the file as soon as it opens, even if nothing is written.
"Ada 10 Bob 8" is written with "w", then "Cara 12 " is added with "a". What does f.read().splitlines() give?
- ['Ada 10', 'Bob 8', 'Cara 12']
- ['Cara 12']
- ['Ada 10', 'Bob 8']
- ['Ada 10', 'Bob 8Cara 12']
Answer: ['Ada 10', 'Bob 8Cara 12']. The file's last line had no , so the added text carries on from it.
A file holds "Buy milk Walk the dog ". first = f.read() and second = f.read(), then print(len(first), repr(second)). What prints?
- 22 ''
- 22 22
- 0 ''
- It stops with an error
Answer: 22 ''. The first read() takes everything; the second starts at the end and gets an empty string.
A file holds "Buy milk Walk the dog". What does f.readlines() give?
- ['Buy milk', 'Walk the dog']
- ['Buy milk ', 'Walk the dog ']
- ['Buy milk ', 'Walk the dog']
- 'Buy milk Walk the dog'
Answer: ['Buy milk ', 'Walk the dog']. Each line keeps its ; the last line has none to keep.
with open("todo.txt", encoding="utf-8") as f: heading = f.readline(). Then, after the block: for line in f: print(line). What happens?
- It prints both lines
- It prints Walk the dog
- It stops with ValueError: I/O operation on closed file.
- It prints nothing
Answer: It stops with ValueError: I/O operation on closed file.. The with block closed the file when it ended, so the loop can't read it.
What does open("todo.txt", "r", "utf-8") do?
- It stops with TypeError: 'str' object cannot be interpreted as an integer
- It opens the file as UTF-8
- It stops with UnicodeDecodeError
- It stops with ValueError: invalid mode
Answer: It stops with TypeError: 'str' object cannot be interpreted as an integer. The third place is buffering, a number. The encoding has to be passed by name: encoding="utf-8".
with open("menu.txt", "w", encoding="utf-8") as f: print(f.write("café ")). What prints?
- 6
- None
- True
- 5
Answer: 5. write() returns the number of characters written: c, a, f, é and the . In UTF-8 the é takes two bytes, but that's still one character.
print("Bob", 8, sep=",", file=f) writes into a file opened with "w". What is the file's text?
- 'Bob 8 '
- 'Bob,8 '
- 'Bob,8'
- "('Bob', 8) "
Answer: 'Bob,8 '. print() turns 8 into text, puts sep between the values and end, a line break, after them, as it does on screen.
Continue this course
- Previous: Checkpoint: A Real-World Script
- Next: Exception Handling — Handle errors gracefully so your programs don't crash unexpectedly
- Quick reference: Python cheat sheet › Files & with
Frequently asked questions
Should I use Path.read_text() or open()?
read_text() and write_text(), from Modern File Paths with pathlib, read or replace a whole small file in one call. Use open() to go through a file line by line, to add to the end, to write in pieces, or to work with bytes.
How do I read a file that's too big for memory?
Loop over the file object, which reads one line at a time, instead of calling read() or readlines(), which load the whole file. Working with Files, Streams & Large Datasets goes further.
How do I read and write the same file?
Read it all, change the text, then open it again with "w" and write it back. The modes "r+" and "a+" read and write through one file object, but "r+" writes over the text from where it is, and "a+" starts at the end, so reading straight away gives an empty string.