Python Read Files

Python gives you several ways to read a file, from pulling in the whole contents at once to looping through it one line at a time.

Reading the contents of a file

To read a file, open it in read mode, which is "r" and is also the default if you omit the mode entirely. The file object then offers several methods for pulling text out. Which one you choose depends on how much of the file you need at once and how large the file is.

Reading the whole file with read()

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

print(content)

Methods for reading

The file object provides a small set of reading methods. Understanding what each one returns helps you avoid loading more into memory than you need.

MethodReturns
read()The entire file as a single string.
read(n)The next n characters as a string.
readline()The next single line, including its newline character.
readlines()A list where each element is one line of the file.

Reading line by line

For large files, reading everything at once can use too much memory. The cleanest and most memory friendly approach is to loop directly over the file object, which yields one line per iteration without holding the whole file at once.

Looping over lines efficiently

with open("notes.txt", "r", encoding="utf-8") as f:
    for line in f:
        # rstrip removes the trailing newline
        print(line.rstrip())
  • Iterating over the file object is the preferred way to process a large file line by line.
  • read() is convenient for small files where you want the full text in one string.
  • readlines() gives you a list you can index or slice, at the cost of loading everything.
  • The rstrip method is handy for trimming the newline that each line normally carries.

Handling a file that is not there

If you try to read a file that does not exist, Python raises FileNotFoundError. Rather than letting that crash your program, wrap the read in a try and except so you can respond with a clear message.

Reading safely with error handling

try:
    with open("missing.txt", "r", encoding="utf-8") as f:
        print(f.read())
except FileNotFoundError:
    print("That file does not exist yet.")
Note: Each open file keeps an internal position. After you read to the end, a second read returns an empty string until you reopen the file or seek back to the start with f.seek(0).
Note: Always pass encoding="utf-8" when reading text. Relying on the system default can produce different results on different machines.