indusai.co

File handling

Programs need to read and write files: configuration, logs, data exports, results. Python makes this straightforward with the built in open() function and the with statement.

Writing a file

python
with open("notes.txt", "w") as f:
    f.write("First line\n")
    f.write("Second line\n")
print("written")
  • "w" opens for writing and replaces any existing content.
  • with guarantees the file is closed when the block ends, even if an error occurs. Always use it.
  • write() does not add a newline; include \n yourself.

Reading a file

python
with open("notes.txt") as f:
    content = f.read()
print(content)

"r" (read) is the default mode. read() returns the entire file as one string.

Reading line by line

For anything large, iterate over the file object. It reads one line at a time.

python
with open("notes.txt") as f:
    for line in f:
        print(line.rstrip())

Each line keeps its trailing \n, hence the rstrip().

readlines() returns a list of all lines; readline() returns the next one.

Appending

"a" adds to the end without erasing.

python
with open("notes.txt", "a") as f:
    f.write("Third line\n")

with open("notes.txt") as f:
    print(f.read())

File modes

ModeMeaning
rRead (default), error if missing
wWrite, create or overwrite
aAppend, create if missing
xCreate, error if it exists
bBinary, combine with the others: rb, wb
+Read and write

Always pass encoding="utf-8" when working with text that might contain non ASCII characters, so behaviour is the same on every operating system.

Handling missing files

python
try:
    with open("does_not_exist.txt") as f:
        print(f.read())
except FileNotFoundError:
    print("No such file")

Working with paths

The pathlib module gives you an object for file paths that works on every operating system.

python
from pathlib import Path

p = Path("notes.txt")
print(p.exists())
print(p.suffix, p.stem)
print(p.read_text())

data_dir = Path("data")
data_dir.mkdir(exist_ok=True)
(data_dir / "out.txt").write_text("hello")
print(sorted(x.name for x in data_dir.iterdir()))

Path.read_text() and write_text() are convenient shortcuts for small files.

Deleting

python
from pathlib import Path
Path("notes.txt").unlink()
print(Path("notes.txt").exists())

CSV files

The csv module handles quoting and commas inside values correctly. Do not split on commas by hand.

python
import csv

rows = [["name", "marks"], ["Asha", 88], ["Bilal", 92]]
with open("marks.csv", "w", newline="") as f:
    csv.writer(f).writerows(rows)

with open("marks.csv", newline="") as f:
    for row in csv.DictReader(f):
        print(row["name"], int(row["marks"]) + 5)

Practice

  1. Write three lines of text to a file, then read it back and print the number of lines.
  2. Append a timestamped line to a log file each time a script runs (use datetime.now()).
  3. Write a CSV of five students and their marks, then read it and print the top scorer.