Files and File Streams

Learning PathSkills: Python, Pathlib, File I/O, Serialization, Encoding, Unicode, PDF, WAV, Context Managers, ZIP Files

Two people working together, one is inputting data on a computer, the other one is reading a long printout

You’ll learn how to handle files in Python from the ground up. Starting with reading and writing files, you’ll use pathlib to navigate the file system and context managers to safely manage resources. From there, you’ll explore serialization, Unicode encodings, and work with ZIP archives, PDFs, and WAV files.

Files and File Streams

Learning Path ⋅ 12 Resources

Reading and Writing Files

Start with the fundamentals of reading and writing files in Python. You’ll learn about file modes, encoding, and practical patterns for working with files.

Title image for Reading and Writing Files in Python (Reading and Writing Files in Python (Guide))

Course

Reading and Writing Files in Python

Learn about reading and writing files in Python. You'll cover everything from what a file is made up of to which libraries can help you along that way. You'll also take a look at some basic scenarios of file usage as well as some advanced techniques.

Title image for Practical Recipes for Working With Files in Python (Working With Files in Python)

Course

Practical Recipes for Working With Files in Python

Learn how you can work with files in Python by using built-in modules to perform practical tasks that involve groups of files, like renaming them, moving them around, archiving them, and getting their metadata.

Navigating the File System

Learn how to navigate and manage the file system using Python’s pathlib module, and safely manage resources with context managers.

Title image for Using Python's pathlib Module (Python 3 "pathlib" Module: Taming The Filesystem)

Course

Using Python's pathlib Module

Learn how to effectively work with file system paths in Python 3 using the pathlib module in the standard library.

Title image for Context Managers and Using Python's with Statement (Context Managers and Python's with Statement)

Course

Context Managers and Using Python's with Statement

Learn what the Python with statement is and how to use it with existing context managers. You'll also learn how to create your own context managers.

Title image for Why Is It Important to Close Files in Python? (Why Is It Important to Close Files in Python?)

Tutorial

Why Is It Important to Close Files in Python?

Model citizens use context managers to open and close file resources in Python, but have you ever wondered why it's important to close files? In this tutorial, you'll take a deep dive into the reasons why it's important to close files and what can happen if you don't.

Serialization and File Downloads

Learn how to serialize Python data for storage and how to download files from the web.

Title image for Serialize Your Data With Python (Serialize Your Data With Python)

Tutorial

Serialize Your Data With Python

In this in-depth tutorial, you'll explore the world of data serialization in Python. You'll compare and use different data serialization formats, serialize Python objects and executable code, and handle HTTP message payloads.

Title image for How to Download Files From URLs With Python (How to Download Files From URLs With Python)

Tutorial

How to Download Files From URLs With Python

In this tutorial, you'll find the right tools to help you download files from URLs with Python and manage the data retrieval process. You'll cover data streaming, thread pools, and asynchronous downloads.

Encoding and Special File Formats

Work with Unicode, character encodings, and specialized file formats like ZIP archives, PDFs, and WAV files.

Title image for Unicode in Python: Working With Character Encodings (Unicode & Character Encodings in Python: A Painless Guide)

Course

Unicode in Python: Working With Character Encodings

In this course, you'll get a Python-centric introduction to character encodings and Unicode. Handling character encodings and numbering systems can at times seem painful and complicated, but this guide is here to help with easy-to-follow Python examples.

Title image for How to Sort Unicode Strings Alphabetically in Python (How to Sort Unicode Strings Alphabetically in Python)

Tutorial

How to Sort Unicode Strings Alphabetically in Python

Learn how to correctly sort Unicode strings in Python while avoiding common pitfalls. You'll explore powerful third-party libraries implementing the complete Unicode Collation Algorithm (UCA), as well as standard library modules and a few handmade solutions.

Title image for Manipulating ZIP Files With Python (Python's zipfile: Manipulate Your ZIP Files Efficiently)

Course

Manipulating ZIP Files With Python

Learn how to manipulate ZIP files using Python's zipfile module from the standard library. Through hands-on examples, you'll learn how to read, write, compress, and extract files from your ZIP files quickly.

Title image for Create and Modify PDF Files in Python (Creating and Modifying PDF Files in Python)

Tutorial

Create and Modify PDF Files in Python

In this tutorial, you'll explore the different ways of creating and modifying PDF files in Python. You'll learn how to read and extract text, merge and concatenate files, crop and rotate pages, encrypt and decrypt files, and even create PDFs from scratch.

Title image for Reading and Writing WAV Files in Python (Reading and Writing WAV Files in Python)

Tutorial

Reading and Writing WAV Files in Python

Learn how to work with WAV audio files in Python using the standard-library wave module. Along the way, you'll synthesize sounds from scratch, visualize waveforms in the time domain, animate real-time spectrograms, and apply special effects to widen the stereo field.

Congratulations on completing this learning path! You’ve learned how to read, write, and manage files in Python, from basic I/O to encodings and specialized file formats.

Continue your intermediate Python journey with the next learning path:

Learning Path

Exceptions, Logging, and Debugging

7 Resources ⋅ Skills: Python, Exceptions, Logging, Debugging, pdb, raise, Built-in Exceptions, Error Handling

You might also be interested in these related learning paths:

Got feedback on this learning path?

Looking for real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

« Browse All Learning Paths