Module 23: Reading from Files

Learn how to access and read data stored in external files using Python.

📂 Accessing External Data

So far, our program's data disappears when it stops running. What if we want to store information permanently or read data created by other programs? That's where files come in!

Reading from files allows our Python scripts to load data from text files, configuration files, datasets, and more. This is fundamental for Data Persistence – making data last beyond a single program run. Let's see how!

🔑 Opening the Door: `open()` and `with`

Before you can read from a file, you need to 'open' it. Python has a built-in function open() for this.

The most common way to open a file for reading is: open('filename.txt', 'r'). The 'r' stands for 'read mode'.

Crucially: Files should always be closed after use to free up resources. The best practice in Python is to use the `with` statement, which handles closing automatically, even if errors occur:

with open('my_data.txt', 'r') as file_object:
    # Code to read from file_object goes here
# File is automatically closed outside the 'with' block

The with statement ensures our interaction with the file is safe and efficient.

The `with open(...)` statement safely opens and prepares the file for reading.

📖 Ways to Read File Content

Once a file is open (inside the `with` block), Python offers several methods to read its contents.

📜

`.read()`

Reads the entire content of the file into a single string. Best for small files where loading everything into memory is okay.

`.readline()`

Reads just one line from the file, including the newline character (`\n`) at the end. Call it again to get the next line. Good for processing line by line without loading the whole file.

📑

`.readlines()`

Reads all lines of the file into a list, where each list item is a string representing one line (including `\n`). Useful if you need random access to lines.

Click on the cards above to reveal more!

⚙️ Reading in Action

Let's see how to read data from a simple file named `data.txt`.

data.txt
Line 1: Hello from the file! Line 2: Python is fun. Line 3: End of data.
with open('data.txt', 'r') as f: content = f.read()  # Read the whole file print(content)

Imagine the text on the left is inside a file named `data.txt`.

The Python code uses with open()Safely opens the file 'data.txt' in read mode ('r') and assigns it to the variable 'f'. Ensures the file is closed afterwards. to open the file.

Inside the `with` block, f.read()The .read() method reads the entire content of the file 'f' into the 'content' variable as a single string. reads all the text from the file into the `content` variable.

Finally, print(content)Displays the value stored in the 'content' variable (which is the text read from the file) to the screen. shows the file's content. This demonstrates basic Input/Output (I/O) - reading data *in* from a file.

🧠 Quick Check!

Module 23 Theory Complete!

You've learned the essentials of reading data from files in Python! This is a vital skill for working with real-world data.
Ready to practice? Head to the Practice Zone or tackle the Advanced Practice.