Reading and writing files

Python · Essentials · 10 min

Most scripts read input files, logs or CSVs and write output somewhere.

The built-in open(path, mode, encoding=...) returns a file object. Always use it in a with block, which closes the file automatically when the block ends, even if an exception occurs:

with open("notes.txt", encoding="utf-8") as f:
    text = f.read()

The mode says what you're doing:

  • "r" read (default), "w" write (truncates an existing file!), "a" append, "x" create but fail if it exists
  • add "b" for binary data such as images: "rb", "wb" — you then read and write bytes, not str

Always pass encoding="utf-8" for text. Without it Python uses the platform's default, which on Windows may not be UTF-8, so the same script can behave differently on another machine.

To read, f.read() returns the whole content as one string; iterating for line in f: streams one line at a time and works for files of any size. Each line keeps its trailing \n, so call line.rstrip("\n") or line.strip(). f.readlines() loads all lines into a list.

To write, f.write(s) writes a string and does not add a newline for you; print(..., file=f) does.

For structured formats, use the standard library instead of hand-parsing: csv.reader/csv.DictReader (open with newline=""), and json.load/json.dump. Later you'll meet pathlib.Path, which offers shortcuts like Path("a.txt").read_text(encoding="utf-8").

Example

import json

with open("todo.txt", "w", encoding="utf-8") as f:
    f.write("buy milk\n")
    print("call mum", file=f)       # print adds the newline

with open("todo.txt", encoding="utf-8") as f:
    for number, line in enumerate(f, start=1):
        print(number, line.rstrip("\n"))
# 1 buy milk
# 2 call mum

with open("todo.txt", "a", encoding="utf-8") as f:
    f.write("water plants\n")

config = {"debug": True, "retries": 3}
with open("config.json", "w", encoding="utf-8") as f:
    json.dump(config, f, indent=2)
with open("config.json", encoding="utf-8") as f:
    print(json.load(f)["retries"])  # 3

Remember

  • Open files in a with block so they always close
  • Pass encoding="utf-8" for text files
  • Iterate for line in f to stream large files

Watch out for

  • Opening an existing file with mode "w" and wiping its contents by accident.

Check yourself

  1. You loop `for line in f:` over a text file. What does each `line` usually end with?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files