Open & read multiple files simultaneously using [with] in Python

Sachin Pal
3 min readAug 18, 2022

--

Photo by Andrea Piacquadio

In computer terminology, a file is a container that stores some information in it.

We use files for future use of our data by permanently storing them on optical drives, hard drives, or other storage devices.

Since we store our data in the files, sometimes we need them to read, write, or see our data.

For such file operations, in Python, we have a built-in function that can help open a file, perform reading and writing operations on a file, and even close the file after the task is completed.

Introduction

This article will discuss how we can open multiple files using the with statement in Python.

Let’s explore the ways and write some code.

1. Using open() function

Well, the first way we will discuss is using Python’s in-built open() function.

If you are familiar with the open() function, you might know that it takes only one file path at a time.

Here’s the syntaxopen(file, mode)

Hence, we cannot pass multiple files path, and if we try to do so, we get an error.

But there is a way we can use the with statement to open and read multiple files using the open() function.

Here’s the code:

Above, we used the open() function for each file wrapped within the with statement.

Then we used the read() function and stored them in the variable.

In the end, we used the for loop to iterate over the contents in each file.

Then we finally print the contents of each file.

What if we have lots of files, which we have to open and read simultaneously, then we have to write more lines of code which, in turn, gets messy, and it’s not a good practice.

Well, we have another way of doing the same operation with lesser lines of code.

2. Using fileinput Module

First, the fileinput module is a built-in module in Python, so you don't need to install it externally.

fileinput - Iterate over lines from multiple input streams

Using the open() function to open multiple files simultaneously can be good, but it's not convenient.

Instead of using the first way, we can use the fileinput module.

Let’s dive into the code and learn the use case:

Since fileinput is a module, we must import this dude to work for us.

We are calling the input() method from the fileinput module and specifying our files into it, which we wrapped within with statement.

Then we use for loop to iterate over the contents of our file.

At last, we are printing the content inside the files.

For more details about the fileinput module, Click here.

The output is the same because we used the same files above.

Note: This module opens the file in read mode only. We cannot open the files in writing mode. It must be one of 'r', 'rU', 'U', and 'rb'.

Conclusion

We carried out the same task but used different approaches to handle them. Both the ways are pretty good.

We learned here two ways to deal with reading and opening multiple files. One used the open() function, and another used the fileinput module.

But every function and module has its pros and cons.

We can’t use Python’s open() function to open multiple files until and unless we use the with statement with it, but if we have lots of files, the code will get messy.

And for the fileinput module, we get the same task done with fewer lines of code, but it can be used only for read mode.

That's all for now…

Keep Coding✌✌.

Originally published at https://geekpython.in.

--

--

Sachin Pal
Sachin Pal

Written by Sachin Pal

I am a self-taught Python developer who loves to write on Python Programming and quite obsessed with Machine Learning.

Responses (2)