Back to Python Notes
Topic #230

Read Files

By the end of this lesson, you will be able to read text and binary files in Python using context managers, handling different reading strategies efficiently.

What it is

File reading in Python involves opening a file object and extracting its contents into memory or processing them line-by-line. The standard approach uses the open() function combined with a with statement (context manager) to ensure files are properly closed after use. Key methods include .read() for entire content, .readline() for single lines, and .readlines() for lists of lines. Binary mode ('rb') is used for non-text data like images or executables.

Why it matters

  • Resource Management: Context managers automatically close files, preventing resource leaks and file locking issues.
  • Memory Efficiency: Iterating over lines avoids loading massive files entirely into RAM.
  • Data Integrity: Correctly distinguishing between text and binary modes prevents encoding errors and data corruption.
  • Simplicity: Python’s built-in functions handle complex OS-level file operations with minimal code.

Syntax or steps

  1. Call open(filename, mode) where mode is 'r' for text or 'rb' for binary.
  2. Wrap the call in a with block: with open(...) as f:.
  3. Use f.read(), f.readline(), or iterate directly over f to access data.
  4. The file closes automatically when the block exits.

Example

# Create a dummy text file for demonstration
with open('example.txt', 'w') as f:
    f.write("Line 1\nLine 2\nLine 3")

# Reading Text Files
print("--- Text File ---")
with open('example.txt', 'r') as f:
    # Method 1: Read all content as one string
    content = f.read()
    print(f"Full Content:\n{content}")

    # Reset pointer to start (since .read() moved it to end)
    f.seek(0)
    
    # Method 2: Read line by line (memory efficient)
    print("\nLine by Line:")
    for line in f:
        print(line.strip()) # strip removes trailing newline

# Reading Binary Files
print("\n--- Binary File ---")
with open('example.txt', 'rb') as f:
    byte_data = f.read(5) # Read first 5 bytes
    print(f"First 5 bytes: {byte_data}")
    print(f"Decoded: {byte_data.decode('utf-8')}")

This example demonstrates three core patterns. First, f.read() loads the whole file into a string variable. Second, iterating over the file object for line in f: reads one line at a time, which is crucial for large files. Third, opening in 'rb' mode returns bytes objects instead of strings, requiring explicit decoding if text representation is needed.

Common mistakes

  • Forgetting to close files: Using f = open(...) without with can leave files open. Always prefer the context manager.
  • Reading past EOF: Calling .readline() repeatedly after the end of the file returns an empty string '', not None. Check for truthiness or length.
  • Mixing modes incorrectly: Trying to decode binary data implicitly or writing text to a binary stream raises TypeError or UnicodeDecodeError.
  • Ignoring newlines: Text files often contain \n. Use .strip() or .rstrip('\n') if clean output is required.

When to use it

Method Best For Memory Usage
f.read() Small files; need full content as string High (loads all)
for line in f: Large files; line-by-line processing Low (one line at a time)
f.readlines() Need random access to lines later Medium (list of all lines)

Practice

Guided Exercise: Write a script that opens data.csv (create one manually with headers and 3 rows), reads it line-by-line, and prints only the second column of each row.

Challenge: Modify the script to handle files larger than your RAM by reading in chunks of 1024 bytes using f.read(1024) in a loop until an empty string is returned.

Quick check

Q: What happens if you call f.read() twice on the same file object without resetting the pointer?

A: The first call reads the entire file and moves the cursor to the end. The second call returns an empty string because there is no more data to read from the current position.

Summary

Python provides robust tools for file reading via the open() function and context managers. Choosing between .read(), iteration, or .readlines() depends on file size and how you intend to process the data. Always match the file mode ('r' vs 'rb') to the data type to avoid encoding errors.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Read Files – FAQs

Quick answers about learning Read Files in Python.

This free note from Coding Now Tech Institute explains Read Files in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Read Files, is 100% free with no signup required.
With focused practice, most students grasp Read Files in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now