Let’s dive in and learn how to read lines from multiple files using Python. To set the stage, let’s create some sample data for our files. We’ll make two text files called “file1.txt” and “file2.txt”. Inside each file, we’ll add some lines of text that we want to read later on. Here’s what they look like:
# Create two text files and write lines of text into them
with open("file1.txt", "w") as file1: # open file1.txt in write mode and assign it to variable file1
file1.write("This is line 1 in file1.\n") # write a line of text into file1
file1.write("This is line 2 in file1.\n") # write a line of text into file1
file1.write("This is line 3 in file1.\n") # write a line of text into file1
with open("file2.txt", "w") as file2: # open file2.txt in write mode and assign it to variable file2
file2.write("This is line 1 in file2.\n") # write a line of text into file2
file2.write("This is line 2 in file2.\n") # write a line of text into file2
file2.write("This is line 3 in file2.\n") # write a line of text into file2
Now that we have our data, let’s write some Python code to read those lines! We’ll use a for loop and the `open()` function to open each file and iterate through its contents one line at a time. Here’s what it looks like:
# This script uses a for loop and the `open()` function to open and read the contents of two files, "file1.txt" and "file2.txt".
# The for loop iterates through the list of filenames, opening each file one at a time.
for filename in ["file1.txt", "file2.txt"]:
# The `with` statement ensures that the file is automatically closed after the code within the block is executed.
with open(filename, 'r') as f:
# The for loop iterates through each line in the file, assigning it to the variable `line`.
for line in f:
# The `print()` function outputs the contents of each line to the console.
print(line)
Let’s break this down a bit. First, we create a list of filenames that we want to read from (in our case, “file1.txt” and “file2.txt”). Then, using the `with open()` function, we open each file in read mode (‘r’) and assign it to a variable called ‘f’.
Next, we use another for loop to iterate through each line of the opened file (which is assigned to ‘line’). Finally, we print out that line.
And there you have it! You’re now reading lines from multiple files like a pro. But let’s be real this code could probably benefit from some error handling and maybe even some comments for clarity. Let’s add those in:
# Loop through a list of file names
for filename in ["file1.txt", "file2.txt"]:
try:
# Open the file in read mode and assign it to the variable 'f'
with open(filename, 'r') as f:
# Iterate through each line of the opened file and print it out
for line in f:
print(line)
# If the file is not found, handle the error and print a message
except FileNotFoundError:
print("File not found:", filename)
In this updated version, we’ve added a `try…except` block to handle any potential errors that might occur when trying to open the file. If the file is not found (i.e., it doesn’t exist), we print out an error message with the name of the missing file.
And there you have it!