Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python fileinput module: read multiple files line by line

The fileinput module in Python provides a convenient way to read and process multiple files line by line, making it especially useful for text processing tasks such as filtering or modifying the content of files. In this tutorial, we will learn how to use the fileinput module to read and process multiple files.

  • Import the fileinput module:

To use the fileinput module, you need to import it first.

import fileinput
  • Reading multiple files:

You can use the fileinput.input() function to read multiple files line by line. The function takes a list of file names as its argument and returns an iterable that can be used in a loop.

import fileinput

file_names = ["file1.txt", "file2.txt", "file3.txt"]

for line in fileinput.input(file_names):
    print(line.strip())

In this example, we have a list of file names, and we use fileinput.input() to read the content of all the files line by line.

  • Using fileinput with a single file:

You can also use the fileinput.input() function to read a single file line by line. Just pass the file name as a string or a list containing a single file name.

import fileinput

file_name = "file1.txt"

for line in fileinput.input(file_name):
    print(line.strip())
  • Processing files in-place:

The fileinput module allows you to modify the content of files in-place, meaning that the original file is overwritten with the modified content. To do this, you need to use the inplace parameter and set it to True.

import fileinput
import sys

file_name = "file1.txt"

with fileinput.input(file_name, inplace=True) as f:
    for line in f:
        modified_line = line.strip().upper()
        sys.stdout.write(modified_line + '\n')

In this example, we read the content of the file file1.txt and convert each line to uppercase. The inplace=True parameter ensures that the changes are written back to the original file. Note that we use sys.stdout.write() to print the modified content, which is required when using the inplace parameter.

  • Accessing metadata:

The fileinput module provides additional information about the current file and line being processed. Some useful attributes and functions are:

  • fileinput.filename(): Returns the name of the current file.
  • fileinput.lineno(): Returns the cumulative line number across all files.
  • fileinput.filelineno(): Returns the current line number in the current file.
  • fileinput.isfirstline(): Returns True if the current line is the first line of the current file, False otherwise.
import fileinput

file_names = ["file1.txt", "file2.txt", "file3.txt"]

for line in fileinput.input(file_names):
    if fileinput.isfirstline():
        print(f"Processing file: {fileinput.filename()}")
    print(f"Line {fileinput.filelineno()}: {line.strip()}")

In this example, we use the fileinput metadata functions to print information about the files and lines being processed.

In summary, the fileinput module in Python is a convenient way to read and process multiple files line by line. You can use the fileinput.input() function to read files and process their content, modify files in-place, and access metadata about the files and lines being processed.

  1. Reading multiple files line by line in Python:

    • Description: Iterate through multiple files and read them line by line using a loop.
    • Code:
      file_paths = ["file1.txt", "file2.txt", "file3.txt"]
      
      for file_path in file_paths:
          with open(file_path, "r") as file:
              for line in file:
                  print(line.strip())
      
  2. How to use fileinput.input() for reading files:

    • Description: The fileinput.input() function simplifies reading lines from multiple files by providing a unified input source.
    • Code:
      import fileinput
      
      file_paths = ["file1.txt", "file2.txt", "file3.txt"]
      
      for line in fileinput.input(files=file_paths):
          print(line.strip())
      
  3. Iterating over lines from multiple files with fileinput:

    • Description: The fileinput module allows iterating over lines from multiple files seamlessly.
    • Code:
      import fileinput
      
      file_paths = ["file1.txt", "file2.txt", "file3.txt"]
      
      for line in fileinput.input(files=file_paths):
          print(line.strip())
      
  4. Specifying files to process with fileinput module in Python:

    • Description: Specify files to process either by providing a list of file paths or using command-line arguments.
    • Code:
      import fileinput
      import sys
      
      # Using a list of file paths
      file_paths = ["file1.txt", "file2.txt"]
      for line in fileinput.input(files=file_paths):
          print(line.strip())
      
      # Using command-line arguments
      for line in fileinput.input():
          print(line.strip())
      
  5. Handling stdin and command-line arguments with fileinput:

    • Description: fileinput can handle both standard input (stdin) and command-line arguments, providing flexibility.
    • Code:
      import fileinput
      
      # Reading from stdin if no files provided
      for line in fileinput.input():
          print(line.strip())
      
      # Reading from files specified in command-line arguments
      for line in fileinput.input():
          print(line.strip())
      
  6. Fileinput and in-place editing of files in Python:

    • Description: Use fileinput with the inplace parameter for in-place editing of files.
    • Code:
      import fileinput
      
      file_path = "example.txt"
      
      with fileinput.input(files=(file_path,), inplace=True) as f:
          for line in f:
              print(line.replace("old", "new"), end="")
      
  7. Customizing behavior with fileinput hooks in Python:

    • Description: Use hooks to customize behavior during fileinput processing, such as changing separators.
    • Code:
      import fileinput
      
      def process_line(line):
          return line.upper()
      
      with fileinput.input(files=("file1.txt",), inplace=True) as f:
          for line in f:
              print(process_line(line), end="")
      
  8. Fileinput module and line numbering in Python:

    • Description: fileinput automatically provides line numbering, which can be accessed using fileinput.lineno().
    • Code:
      import fileinput
      
      for line in fileinput.input(files=("file1.txt", "file2.txt")):
          print(f"Line {fileinput.lineno()}: {line.strip()}")
      
  9. Using fileinput with regular expressions in Python:

    • Description: Apply regular expressions to filter lines during processing with fileinput.
    • Code:
      import fileinput
      import re
      
      pattern = re.compile(r"\d+")
      
      for line in fileinput.input(files=("file1.txt", "file2.txt")):
          if pattern.search(line):
              print(line.strip())