Reading Files in Python

A cartoon owl with wide eyes stands against an orange background, holding and reading a letter. Above it, bold text reads “READING FILES IN PYTHON.”

For this tutorial you can use a text editor such as notepad (on Windows) to create a text file to read.

Opening a File the Safe Way

The golden rule is to use a context manager (with ... as ...). It opens the file, lets you read from it, and automatically closes it when finished, even if something goes wrong.

This is the cleanest and safest way to read a file.

Try this:

  • Create a small file and read it.
  • Change the filename to something that doesn’t exist to see the error.

Different Ways to Read

You can read the whole file, just one line, or loop over it.

Try this:

  • Count how many lines are in a file.
  • Print only the lines containing the word LiddleBit.

Common Exceptions

When working with files, a few errors are common:

  • FileNotFoundError: the file doesn’t exist.
  • PermissionError: you don’t have access.
  • IsADirectoryError: you gave a folder instead of a file.

These are normal and can be handled with try…except to keep your program from crashing.

Main Topic

Python Files and Directories

A cartoon owl with wide eyes stands in front of grey filing cabinets. One drawer is open, filled with folders, and the owl is holding a single document. The background is a warm orange tone, and the words “PYTHON FILES” appear in large bold text above the cabinets.

A light-hearted intro to handling files and directories in Python, featuring an owl mascot and fun “Python Files” imagery.

Other Tutorials in this Topic

A cartoon owl shrugs at a table with three letters: one in English, one in Arabic, and one in Chinese. Above it, bold text reads “CHARACTER ENCODINGS.”

Reading different Character Sets

This tutorial explains character encodings, why they exist, handling decoding errors, platform newline differences, and reading binary…

A cheerful cartoon owl writes in an open notebook with a quill pen beside a filing cabinet drawer labelled “w, a, x.” Above, bold text reads “Writing Files in Python.”

Writing to Files in Python

Part 1 introduces writing to files in Python using context managers, explains file modes w, a, x,…

A cartoon owl sits at a desk holding two papers. One shows “Café 😊” clearly, the other displays scrambled symbols. Behind, a chalkboard reads “Character Encoding.”

Writing different Character Sets

This tutorial explains character encoding, why encodings differ, handling Unicode errors, appending safely, and using safer update…

A cartoon owl takes a document from a filing cabinet and drops it into a bin. Above, bold text reads “File Operations.”

Renaming and Deleting Files in Python

This tutorial covers renaming, moving, and deleting files in Python using pathlib, with examples, common exceptions, and…

A cartoon owl stands in front of a bookshelf, holding an old book while placing a new one on the shelf. Above, bold text reads “File Operations.”

More Python File Operations

This tutorial explores safer file operations, including overwriting with os.replace, moving across drives, creating backups, soft deletes,…

A cartoon owl stands at a crossroads holding a map. Signposts point to Linux (/home/), Windows (C:\Users), and Reports. Above, bold text reads “Navigating Directories in Python.”

Navigating Directories in Python

This tutorial explains navigating directories in Python with pathlib, covering creating, listing, deleting folders, handling exceptions, and…