Python HowTo – Using the Python fileinput Module

How To Use The Python Fileinput Module

Hello everyone! In this article, we’ll be taking a look at using the Python fileinput Module. This is a very handy utility module for quickly going through a list of files as input.

Let’s look at how we can use this module to effectively iterate through input files.


How to use the Python fileinput module

This is a part of the Python standard library, so there is no need to pip install this module.

To import this module, we can use the below statement:

import fileinput

Usually, if you want to do some IO operations (read/write) for a single input file, we generally use the open() function to achieve this.

However, if your need to pass multiple files, we can directly loop over all of them quickly using fileinput.

Let’s look at an example now.

1. Reading Multiple Files

The primary use for this module is to use the fileinput.FileInput instance as a context manager.

import fileinput

with fileinput.FileInput(files=('a.txt', 'b.txt'), mode='r') as input:
    ...

Here, we can pass as many files as we wish to the files keyword argument. A single file is also allowed.

To specify the mode for opening the files, we must specify the keyword argument mode.

Assume that our directory has the following two files a.txt and b.txt with the following content:

$ cat a.txt
Hello from AskPython!
This is a.txt

$ cat b.txt
Hello from AskPython!
this is b.txt

Now, we’ll be passing both these files as input to our sample program:

import fileinput

with fileinput.FileInput(files=('a.txt', 'b.txt'), mode='r') as input:
    for line in input:
        print(line)

Output

Hello from AskPython!

This is a.txt

Hello from AskPython!

This is b.txt

Indeed, we were able to get both the files printed! The space between each line is due to print() adding a newline after each statement. Since our file already has new lines, it will print an additional line in between.

2. Verifying First Lines and Reading File Names

Now, this module has other methods that we can use to our advantage.

If you want to look at the name of the file currently being read, we can use the fileinput.filename() method.

However, this will return None if no line has yet been read! So you can use this only after the first read.

If we want to find out the name of the file being read, we can use one more flag.

The fileinput.isfirstline() method will return True if the line read is the first line! So we can print to the console if this flag is True.

Here is a simple example, using the same program for a.txt and b.txt

import fileinput

with fileinput.FileInput(files=('a.txt', 'b.txt'), mode='r') as input:
    for idx, line in enumerate(input):
        if input.isfirstline() == True:
            # We will indicate the file name being read if the first line is read
            print(f'Reading file {input.filename()}...')
        print(line)

Output

Reading file a.txt...
Hello from AskPython!

This is a.txt

Reading file b.txt...
Hello from AskPython!

This is b.txt

As you can see, we are able to view the name of the file being read, when the first line is beign read.

Similarly, we can use other helper methods to quickly iterate through our input files.

To see more, you can view the documentation.


Conclusion

In this article, we learned how we could use the fileinput module in Python to quickly loop over our input files from stdin.

References