Reading files is a necessary task in any programming language. Whether it’s a database file, image, or chat log, having the ability to read and write files greatly enhances what we can with Python.
Before we can create an automated audiobook reader or website, we’ll need to master the basics. After all, no one ever ran before they crawled.
To complicate matters, Python offers several solutions for reading files. We’re going to cover the most common procedures for reading files line by line in Python. Once you’ve tackled the basics of reading files in Python, you’ll be better prepared for the challenges that lie ahead.
You’ll be happy to learn that Python provides several functions for reading, writing, and creating files. These functions simplify file management by handling some of the work for us, behind the scenes.
We can use many of these Python functions to read a file line by line.
Read a File Line by Line with the readlines() Method
Our first approach to reading a file in Python will be the path of least resistance: the readlines() method. This method will open a file and split its contents into separate lines.
This method also returns a list of all the lines in the file. We can use readlines() to quickly read an entire file.
For example, let’s say we have a file containing basic information about employees at our company. We need to read this file and do something with the data.
employees.txt
Name: Marcus Gaye
Age: 25
Occupation: Web Developer
Name: Sally Rain
age: 31
Occupation: Senior Programmer
Example 1: Using readlines() to read a file
# open the data file
file = open("employees.txt")
# read the file as a list
data = file.readlines()
# close the file
file.close()
print(data)
Output
['Name: Marcus Gaye\n', 'Age: 25\n', 'Occupation: Web Developer\n', '\n', 'Name: Sally Rain\n', 'age: 31\n', 'Occupation: Senior Programmer\n']
readline() vs readlines()
Unlike its counterpart, the readline() method only returns a single line from a file. The realine() method will also add a trailing newline character to the end of the string.
With the readline() method, we also have the option of specifying a length for the returned line. If a size is not provided, the entire line will be read.
Consider the following text file:
wise_owl.txt
A wise old owl lived in an oak.
The more he saw the less he spoke.
The less he spoke the more he heard.
Why can’t we all be like that wise old bird?
We can use readline() to get the first line of the text document. Unlike readlines(), only a single line will be printed when we use the readline() method to read the file.
Example 2: Read a single line with the readline() method
file = open("wise_owl.txt")
# get the first line of the file
line1 = file.readline()
print(line1)
file.close()
Output
A wise old owl lived in an oak.
The readline() method only retrieves a single line of text. Use readline() if you need to read all the lines at once.
file = open("wise_owl.txt")
# store all the lines in the file as a list
lines = file.readlines()
print(lines)
file.close()
Output
[‘A wise old owl lived in an oak.\n’, ‘The more he saw the less he spoke.\n’, ‘The less he spoke the more he heard.\n’, “Why can’t we all be like that wise old bird?\n”]
Using a While Loop to Read a File
It’s possible to read a file using loops as well. Using the same wise_owl.txt file that we made in the previous section, we can read every line in the file using a while loop.
Example 3: Reading files with a while loop and readline()
file = open("wise_owl.txt",'r')
while True:
next_line = file.readline()
if not next_line:
break;
print(next_line.strip())
file.close()
Output
A wise old owl lived in an oak.
The more he saw the less he spoke.
The less he spoke the more he heard.
Why can’t we all be like that wise old bird?
Beware of infinite loops
A word of warning when working with while loops is in order. Be careful to add a termination case for the loop, otherwise you’ll end up looping forever. Consider the following example:
while True:
print("Groundhog Day!")
Executing this code will cause Python to fall into an infinite loop, printing “Groundhog Day” until the end of time. When writing code like this, always provide a way to exit the loop.
If you find that you’ve accidentally executed an infinite loop, you can escape it in the terminal by tapping Control+C on your keyboard.
Reading a file object in Python
It’s also possible to read a file in Python using a for loop. For example, our client has given us a list of addresses of previous customers. We need to read the data using Python.
Here’s the list of clients:
address_list.txt
Bobby Dylan
111 Longbranch Ave.
Houston, TX 77016
Sam Garfield
9805 Border Rd.
New Brunswick, NJ 08901
Penny Lane
408 2nd Lane
Lindenhurst, NY 11757
Marcus Gaye
622 Shub Farm St.
Rockledge, FL 32955
Prudence Brown
66 Ashley Ave.
Chaska, MN 55318
Whenever we open a file object, we can use a for loop to read its contents using the in keyword. With the in keyword, we can loop through the lines of the file.
Example 4: Using a for loop to read the lines in a file
# open the file
address_list = open("address_list.txt",'r')
for line in address_list:
print(line.strip())
address_list.close()
Unfortunately, this solution will not work for our client. It’s very important that the data is in the form of a Python list. We’ll need to break the file into separate addresses and store them in a list before we can continue.
Example 5: Reading a file and splitting the content into a list
file = open("address_list.txt",'r')
address_list = []
i = 0
current_address = ""
for line in file:
# add a new address every three lines
if i > 2:
i = 0
address_list.append(current_address)
current_address = ""
else:
# add the line to the current address
current_address += line
i += 1
# use a for-in loop to print the list of addresses
for address in address_list:
print(address)
file.close()
Reading a file with the Context Manager
File management is a delicate procedure in any programming language. Files must be handled with care to prevent their corruption. When a file is opened, care must be taken to ensure the resource is later closed.
And there is a limit to how many files can be opened at once in Python. In order to avoid these problems, Python provides us with the Context Manager.
Introducing the with block
Whenever we open a file in Python, it’s important that we remember to close it. A method like readlines() will work okay for small files, but what if we have a more complex document? Using Python with statements will ensure that files are handled safely.
- The with statement is used for safely accessing resource files.
- Python creates a new context when it encounters the with block.
- Once the block executes, Python automatically closes the file resource.
- The context has the same scope as the with statement.
Let’s practice using the with statement by reading an email we have saved as a text file.
email.txt
Dear Valued Customer,
Thank you for reaching out to us about the issue with the product that you purchased. We are eager to address any concerns you may have about our products.
We want to make sure that you are completely satisfied with your purchase. To that end, we offer a 30-day, money-back guarantee on our entire inventory. Simply return the product and we’ll happily refund the price of your purchase.
Thank you,
The ABC Company
code example
# open file
with open("email.txt",'r') as email:
# read the file with a for loop
for line in email:
# strip the newline character from the line
print(line.strip())
This time a for loop is used to read the lines of the file. When we’re using the Context Manager, the file is automatically closed when it’s handler goes out of scope. When the function is finished with the file, with statement ensures the resource is handled responsibly.
Summary
We’ve covered several ways of reading files line by line in Python. We’ve learned there is a big difference between the readline() and readlines() methods, and that we can use a for loop to read the contents of a file object.
We also learned how to use the with statement to open and read files. We saw how the context manager was created to make handling files safer and easier in Python.
Several examples were provided to illustrated the various forms of file handling available in Python. Take time to explore the examples and don’t be afraid to experiment with the code if you don’t understand something.
If you’d like to learn about programming with Python, follow the links below to view more great lessons from Python for Beginners.
Related Posts
- Using Python try except to handle errors
- How to use Python split string to improve your coding skills
- Using Python string concatenation for better readability
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.