Lesson 11.1: Reading Files
What you will learn:
- How to open a file with
open() - Reading entire file contents with
read() - Reading one line at a time with
readline() - Reading all lines as a list with
readlines() - Iterating through lines in a file
- Understanding file paths
1. Opening Files with open()
To work with a file in Python, you first need to open it. The open() function creates a connection between your program and a file on your computer.
mode parameter specifies what you want to do with the file. For reading, use "r" (this is the default).
# Open a file for reading file = open("greeting.txt", "r") # "r" is the default, so this also works: file = open("greeting.txt") # Always close the file when done file.close()
Think of opening a file like opening a book. You need to open it before you can read it, and you should close it when you are done.
FileNotFoundError. Make sure the file exists and the path is correct.
2. Reading Entire Contents with read()
The read() method reads the entire file as a single string:
Suppose greeting.txt contains:
Hello, World! Welcome to Python. Have a great day!
Reading the Entire File
file = open("greeting.txt", "r") content = file.read() print(content) file.close()
Welcome to Python.
Have a great day!
The read() method returns everything in the file as one big string, including the newline characters (\n) between lines.
3. Reading One Line with readline()
The readline() method reads one line at a time. Each call reads the next line:
Reading Line by Line
file = open("greeting.txt", "r") line1 = file.readline() print("Line 1:", line1.strip()) line2 = file.readline() print("Line 2:", line2.strip()) line3 = file.readline() print("Line 3:", line3.strip()) file.close()
Line 2: Welcome to Python.
Line 3: Have a great day!
\n) at the end. The strip() method removes this whitespace so your output looks clean.
4. Reading All Lines as a List with readlines()
The readlines() method reads all lines and returns them as a list of strings:
Getting a List of Lines
file = open("greeting.txt", "r") lines = file.readlines() print(lines) file.close()
Notice each line still has \n at the end. You can process the list to clean them up:
file = open("greeting.txt", "r") lines = file.readlines() file.close() # Clean up each line for line in lines: print(line.strip())
5. Iterating Through a File
The most Pythonic and memory-efficient way to read a file line by line is to iterate directly over the file object:
Looping Through Lines (Recommended)
file = open("greeting.txt", "r") for line in file: print(line.strip()) file.close()
Welcome to Python.
Have a great day!
This approach is best for large files because it only loads one line into memory at a time, rather than loading the entire file at once.
6. File Paths
When opening a file, you need to tell Python where the file is located. There are two types of paths:
Relative vs. Absolute Paths
# Relative path - relative to where your script is running file = open("data.txt") # Same folder file = open("data/scores.txt") # Subfolder called "data" file = open("../notes.txt") # Parent folder # Absolute path - full path from the root # Mac/Linux: file = open("/Users/alice/data.txt") # Windows (use raw string to avoid backslash issues): file = open(r"C:\Users\alice\data.txt")
r"...") or forward slashes for file paths to avoid issues with backslashes being interpreted as escape characters.
Check Your Understanding
What is the difference between read(), readline(), and readlines()?
read() returns the entire file as a single string. readline() returns one line as a string (the next unread line). readlines() returns all lines as a list of strings. For large files, iterating over the file object directly (for line in file) is the most memory-efficient approach.
Key Takeaways
- Use
open(filename, "r")to open a file for reading read()returns the entire file as one stringreadline()returns one line at a timereadlines()returns all lines as a list- Use
strip()to remove trailing newline characters - Always close files with
file.close()when done - Iterating directly over the file object is the most efficient approach