Reading Files

Reading files is one of the most common operations in programming. Whether you're loading configuration, processing data, or analyzing logs, you need to get file contents into your program.

The Basic Pattern

Reading a file involves three steps: open it, read the contents, and close it. Python's with statement handles opening and closing automatically:

with open("data.txt", "r") as f:
    contents = f.read()
    print(contents)

The "r" means "read mode." The with statement ensures the file closes properly, even if an error occurs. This prevents resource leaks and is the recommended approach.

Reading All at Once

The read() method loads the entire file into a single string:

with open("story.txt", "r") as f:
    entire_text = f.read()
    print(entire_text)

This works well for small files. For very large files, loading everything at once might use too much memory.

Reading Line by Line

For larger files or when you need to process each line separately, iterate over the file directly:

with open("data.txt", "r") as f:
    for line in f:
        print(line.strip())  # strip() removes the newline character

Each line includes a newline character (\n) at the end. The strip() method removes it along with any extra whitespace.

You can also use readlines() to get all lines as a list:

with open("data.txt", "r") as f:
    lines = f.readlines()
    print(f"File has {len(lines)} lines")

Handling Text Encoding

Text files use encoding to represent characters as bytes. UTF-8 is the most common encoding and handles international characters well:

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

If you see strange characters or get encoding errors, specifying the correct encoding often fixes it.

What Happens Without with

You can open files without with, but you must remember to close them:

f = open("data.txt", "r")
contents = f.read()
f.close()  # Don't forget this!

If your program crashes before close(), the file stays open. The with statement prevents this problem — always prefer it.

See More

Further Reading

You need to be signed in to leave a comment and join the discussion