Python fileinput Module

The fileinput module in Python provides a simple way to iterate over lines from multiple input streams, including standard input, and optionally make in-place modifications to files. It is particularly useful for writing scripts that process files line-by-line.

Table of Contents

  1. Introduction
  2. Key Functions and Methods
    • input
    • filename
    • lineno
    • filelineno
    • isfirstline
    • isstdin
    • nextfile
    • close
  3. Examples
    • Basic Usage
    • Processing Multiple Files
    • In-Place File Editing
  4. Real-World Use Case
  5. Conclusion
  6. References

Introduction

The fileinput module allows you to loop over the lines of multiple input streams easily. It can handle standard input, files specified on the command line, or a combination of both. The module provides utility functions and methods to control and inspect the input streams.

Key Functions and Methods

input

The input function is the primary entry point for the module. It returns an iterator that yields lines from the input streams.

import fileinput

for line in fileinput.input():
    print(line, end='')

filename

Returns the name of the file currently being read.

import fileinput

for line in fileinput.input(files=('file1.txt', 'file2.txt')):
    print(f"Filename: {fileinput.filename()}, Line: {line}", end='')

lineno

Returns the cumulative line number of the line that has just been read.

import fileinput

for line in fileinput.input():
    print(f"Line {fileinput.lineno()}: {line}", end='')

filelineno

Returns the line number in the current file.

import fileinput

for line in fileinput.input():
    print(f"File Line {fileinput.filelineno()}: {line}", end='')

isfirstline

Returns True if the line just read is the first line of its file, otherwise False.

import fileinput

for line in fileinput.input():
    if fileinput.isfirstline():
        print(f"First line of {fileinput.filename()}")
    print(line, end='')

isstdin

Returns True if the line was read from standard input, otherwise False.

import fileinput

for line in fileinput.input():
    if fileinput.isstdin():
        print("Reading from standard input")
    print(line, end='')

nextfile

Skips the rest of the current file, closing it and moving on to the next file.

import fileinput

for line in fileinput.input():
    if 'skip' in line:
        fileinput.nextfile()
    print(line, end='')

close

Closes the sequence of input streams, preventing further iteration.

import fileinput

for line in fileinput.input():
    if 'stop' in line:
        fileinput.close()
    print(line, end='')

Examples

Basic Usage

import fileinput

for line in fileinput.input(files=('file1.txt', 'file2.txt')):
    print(line, end='')

Processing Multiple Files

import fileinput

for line in fileinput.input(files=('file1.txt', 'file2.txt')):
    if fileinput.isfirstline():
        print(f"\nProcessing file: {fileinput.filename()}")
    print(f"Line {fileinput.filelineno()}: {line}", end='')

In-Place File Editing

import fileinput

for line in fileinput.input(files=('file1.txt',), inplace=True):
    print(line.replace('old_text', 'new_text'), end='')

Real-World Use Case

Log File Analysis

import fileinput

error_count = 0

for line in fileinput.input(files=('log1.txt', 'log2.txt')):
    if 'ERROR' in line:
        error_count += 1
        print(f"Error in {fileinput.filename()}, Line {fileinput.filelineno()}: {line}", end='')

print(f"\nTotal Errors: {error_count}")

Conclusion

The fileinput module in Python provides a powerful yet simple interface for processing lines from multiple input streams, including standard input. It supports features like cumulative line numbering, file-specific line numbering, and in-place file editing, making it used for writing scripts that handle file input.

References

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare