Sahaj.blogs
29 Sept 2023 · StudyPhase 1 · Python

Python · Files: reading and writing

Opening files with with-blocks, text vs binary modes, encodings, reading line by line, and pathlib.

open(path, mode) returns a file object. The modes I need most:

modemeaning
"r"read text (the default)
"w"write text; truncates the file
"a"append
"rb" / "wb"read/write bytes

Always use with open(...) as f:. It closes the file even if an exception happens.

Pass encoding="utf-8" explicitly. On Windows the default text encoding can be a locale code page, so a file that reads fine on Linux can break here.

Iterating over a file object yields one line at a time, so even a huge file never sits in memory all at once. pathlib.Path makes path handling cleaner than string concatenation.

from pathlib import Path
import tempfile

folder = Path(tempfile.mkdtemp())
p = folder / "notes.txt"

with open(p, "w", encoding="utf-8") as f:
    f.write("line one\n")
    f.write("line two – with a dash\n")

with open(p, "a", encoding="utf-8") as f:
    print("line three", file=f)

with open(p, encoding="utf-8") as f:
    for i, line in enumerate(f, 1):
        print(i, line.rstrip())

print(p.name, p.suffix, p.stat().st_size, "bytes")
print(p.read_text(encoding="utf-8").count("\n"), "lines")
print(sorted(x.name for x in folder.iterdir()))

Output:

1 line one
2 line two � with a dash
3 line three
notes.txt .txt 48 bytes
3 lines
['notes.txt']

#python#files

Sources

  1. Reading and Writing Files — Python Software Foundation
  2. pathlib: object-oriented filesystem paths — Python Software Foundation
  3. Built-in functions: open — Python Software Foundation