Skip to content

File Handling in Python

Read and write files with open(), modes, the with statement, and path operations with pathlib.

What you'll learn

  • Open and close files with open() and close()
  • Use the with statement for automatic resource cleanup
  • Read and write text and binary files
  • Iterate over file lines efficiently
  • Work with paths using the pathlib module

Concept

Opening Files

The built-in open() function opens a file and returns a file object. You must specify a mode:

f = open("data.txt", "r")   # read (default)
f = open("data.txt", "w")   # write (truncates file)
f = open("data.txt", "a")   # append
f = open("data.txt", "rb")  # read binary

| Mode | Description | |------|-------------| | r | Read (default) — file must exist | | w | Write — creates or truncates | | a | Append — creates or adds to end | | r+ | Read and write | | b | Binary mode (add to above) |

Always close files when done: f.close(). However, the with statement is strongly preferred because it guarantees the file is closed even if an error occurs.

The with Statement

# BEST PRACTICE — file auto-closes even on error
with open("data.txt", "r") as f:
    content = f.read()
# f is closed here automatically

The with statement calls f.__enter__() on entry and f.__exit__() on exit, which handles closing. This is the only recommended way to work with files — never use bare open() without with in production code.

Reading Files

# Read entire file as one string
with open("data.txt") as f:
    content = f.read()

# Read all lines into a list
with open("data.txt") as f:
    lines = f.readlines()

# Iterate line by line — memory efficient for large files
with open("data.txt") as f:
    for line in f:
        print(line.strip())

Iterating line by line (for line in f) is the most memory-efficient way to process large files — it reads one line at a time instead of loading the entire file into memory.

Writing Files

# Write text
with open("output.txt", "w") as f:
    f.write("Hello, World!\n")
    f.write("Second line\n")

# Write multiple lines at once
lines = ["line 1\n", "line 2\n", "line 3\n"]
with open("output.txt", "w") as f:
    f.writelines(lines)

Note that .write() does not add a newline automatically — you must include \n yourself. The "w" mode truncates the file; use "a" to append without erasing.

Encoding

Always specify encoding for text files to avoid platform-dependent defaults:

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

Without encoding="utf-8", Python uses the system default (often cp1252 on Windows), which can cause UnicodeDecodeError when reading files created on other systems.

pathlib — Modern Path Handling

The pathlib module provides an object-oriented way to work with paths that is cleaner than os.path:

from pathlib import Path

p = Path("data") / "file.txt"   # platform-independent path joining
print(p.exists())               # check if file exists
print(p.suffix)                 # '.txt'
print(p.stem)                   # 'file'
print(p.parent)                 # 'data'

# Read with pathlib — opens and closes automatically
content = p.read_text(encoding="utf-8")
p.write_text("new content", encoding="utf-8")

# List files in a directory
for child in Path(".").iterdir():
    print(child)

pathlib is the modern, recommended way to handle paths. It works across operating systems and eliminates the need for os.path.join() string concatenation.

Example

              # Writing a file with the with statement
with open("output.txt", "w", encoding="utf-8") as f:
    f.write("Hello, World!\n")
    f.write("Second line\n")
    f.writelines(["line 3\n", "line 4\n"])

# Reading the entire file
with open("output.txt", "r", encoding="utf-8") as f:
    content = f.read()
print(content)

# Reading line by line — memory efficient
print("Lines:")
with open("output.txt", encoding="utf-8") as f:
    for line in f:
        print(f"  {line.strip()}")

# Appending to a file
with open("output.txt", "a", encoding="utf-8") as f:
    f.write("Appended line\n")

# pathlib for modern path handling
from pathlib import Path

p = Path("output.txt")
print(f"Exists: {p.exists()}")
print(f"Suffix: {p.suffix}")
print(f"Size: {p.stat().st_size} bytes")

# Read via pathlib
text = p.read_text(encoding="utf-8")
print(f"First 20 chars: {text[:20]}")
            

Shows writing with the with statement, reading entire content and line-by-line, appending, and using pathlib for path inspection and reading. The with statement ensures files are always closed properly.

Try it

  • JSON Formatter

    Reading and writing JSON files is a common file I/O task.

  • CSV to JSON

    CSV file reading and JSON conversion — practical file handling.

Common mistakes

The mistake

Forgetting to close files when using open() without with

The fix

Always use the with statement: with open('file') as f:. It automatically closes the file even if an exception occurs. Never rely on calling f.close() manually — it can be skipped if an error happens.

The mistake

Not specifying encoding='utf-8' when reading text files

The fix

Without explicit encoding, Python uses the system default which varies by platform. This causes UnicodeDecodeError on files from other systems. Always pass encoding='utf-8' for text files.

Related Cheatsheets

Practice File Handling in Python

4 exercises

Practice File Handling in Python

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors