Python: File Handling

Learn how to read, write, and manage files in Python. From basic text files to CSV and JSON data, file I/O is a fundamental skill for building real-world applications.

Opening Files with open()

Python's built-in open() function is the entry point for all file operations. It takes the file path and a mode string as arguments and returns a file object through which you read or write data.

File Modes Reference

The mode argument tells Python what you intend to do with the file.

  • "r" - Read (default). Raises FileNotFoundError if file is missing.
  • "w" - Write. Creates the file if it does not exist; truncates to zero if it does.
  • "a" - Append. Writes at the end without discarding existing content.
  • "r+" - Read and write. File must already exist.
  • "w+" - Read and write. Truncates the file first.
  • "rb" - Read in binary mode (images, PDFs, executables).
  • "wb" - Write in binary mode.

Basic open() Call

Python

Opening a file and always remembering to close it.

Reading Files

Python gives you three reading methods, each suited to a different scenario.read() loads the entire file into a single string, which is fine for small files.readline()reads one line at a time, andreadlines()returns all lines as a list. For large files, iterating over the file object directly is the most memory-efficient approach.

Three Ways to Read

Python

Choosing the right read method for your use case.

Writing Files

write()takes a single string and writes it to the file.writelines()takes a list of strings and writes them sequentially without adding newlines between them automatically. You must include the newline characters yourself.

Writing String Data

Python

Creating and populating a text file.

Context Manager: the with Statement

The with statement is the recommended way to work with files in Python. It guarantees the file is closed properly when the block ends, whether the code completed normally or raised an exception. This eliminates resource leaks without requiring explicitclose() calls.

Why with Is Always Preferred

Forgetting to close a file can lead to data corruption and resource leaks.

  • The file is closed automatically when the with block exits
  • Works even if an exception is raised inside the block
  • Cleaner code with less indentation compared to try/finally
  • You can open multiple files in one with statement: with open(a) as f, open(b) as g:

Context Manager Pattern

Python

The idiomatic way to work with files.

File Pointer: seek() and tell()

Every open file has an internal cursor called the file pointer that marks where the next read or write will happen.tell()returns the current byte position, andseek()moves it. This is useful when you need to re-read part of a file or jump to a specific location without reopening it.

Navigating Within a File

Python

Moving the cursor to read sections selectively.

Working with CSV Files

The csv module handles the parsing of comma-separated value files, including edge cases like quoted fields containing commas. Usecsv.reader for simple row access and csv.DictReaderwhen you want each row as a dictionary keyed by column headers.

Reading and Writing CSV

Python

Parsing structured tabular data.

Working with JSON Files

JSON is the universal format for exchanging data between APIs and services. Python's json module provides four core functions:load() to parse a file, loads()to parse a string, dump()to write to a file, and dumps()to produce a string.

JSON Read and Write

Python

Serializing Python objects to disk and back.

Working with Binary Files

Binary mode (rb,wb) reads and writes raw bytes rather than decoded text. Use it for images, audio, PDFs, executables, or any non-text format. The data you receive is abytes object rather than a string.

Copying a Binary File

Python

Reading and writing raw bytes.

The os and os.path Modules

The os module gives you access to operating system functions: creating directories, listing contents, renaming and deleting files, and querying environment variables.os.pathprovides path manipulation utilities that work correctly across Windows, macOS, and Linux.

File System Operations with os

Python

Creating, checking, listing, and removing paths.

The pathlib Module

Introduced in Python 3.4, pathlibtreats file paths as objects rather than plain strings. This makes path manipulation more readable and eliminates the need to callos.path.join()by hand. It is now the preferred approach in modern Python code.

Modern Path Handling

Python

Object-oriented file system manipulation.

The shutil Module

While oshandles low-level operations, shutil(shell utilities) provides higher-level operations: copying files with or without metadata, moving files between directories, and deleting entire directory trees. It is what you reach for when os.remove()is not enough.

High-Level File Operations

Python

Copying, moving, and removing with shutil.

Quiz - Test Your Knowledge

Work through these eight questions covering file modes, context managers, and the standard library tools for file and directory management.

Knowledge Check

1. Which file mode opens a file for reading and raises an error if the file does not exist?

2. What is the primary advantage of using the "with" statement to open files?

3. What does readline() return when it reaches the end of a file?

4. Which method moves the file pointer to a specific byte position?

5. Which module is best suited for reading and writing structured tabular data?

6. What does json.dumps() do?

7. Which pathlib method checks if a path points to an existing file?

8. Which shutil function copies a file along with its metadata?