Fundamentals 9 min read

Using Python’s fileinput Module for Elegant File Reading and Processing

This article introduces Python’s built‑in fileinput module, explains how it iterates over command‑line files or standard input, details its key functions and parameters, and provides multiple practical code examples for reading, modifying, and timestamping file contents.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using Python’s fileinput Module for Elegant File Reading and Processing

The fileinput module in Python provides a helper class and several functions that simplify reading from standard input or a list of files, automatically iterating over each line and handling file opening modes.

When no file list is supplied, it reads from sys.argv[1:]; if the list is empty it falls back to sys.stdin. Files are opened in text mode by default, but the mode argument can be passed to input() or FileInput to change this behavior.

If sys.stdin is used more than once, subsequent reads return no lines unless the stream is reset (e.g., sys.stdin.seek(0)) or used interactively.

Key functions and methods include:

ID

Function

Description

1 fileinput.input() Returns an iterator suitable for a for loop; creates a FileInput instance.

2 fileinput.filename() Returns the name of the currently read file.

3 fileinput.fileno() Returns the file descriptor of the current file as an integer.

4 fileinput.lineno() Returns the number or index of lines read so far.

5 fileinput.filelineno() Returns the line number within the current file.

6 fileinput.isfirstline() Checks whether the current line is the first line of the file.

7 fileinput.isstdin() Determines if the last line was read from stdin.

8 fileinput.nextfile() Closes the current file and moves to the next one.

9 fileinput.close() Closes the FileInput object.

10 fileinput.hook_compressed() Transparent opening of gzip and bzip2 compressed files using the gzip and bz2 modules.

11 fileinput.hook_encoded() Provides an open hook that reads each file with a specified encoding and error handling.

Typical usage examples:

# Simple iteration
import fileinput
for line in fileinput.input():
    process(line)

# Using FileInput as a context manager
with fileinput.input(files=('spam.txt', 'eggs.txt')) as f:
    for line in f:
        process(line)

The fileinput.input() function accepts several parameters:

fileinput.input(files=None, inplace=False, backup='', *, mode='r', openhook=None)

files : list of file paths.

inplace : if True, redirects standard output back to the file (default False).

backup : extension for backup files.

bufsize : buffer size (default 0).

mode : file opening mode, default 'r'.

openhook : custom hook for opening files (e.g., encoding).

Examples of practical tasks:

# Print filename and line number
import fileinput
for line in fileinput.input():
    filename = fileinput.filename()
    lineno = fileinput.lineno()
    print(f"{filename} | Line Number: {lineno} | {line}")
# In‑place editing with a processing function
import fileinput

def process(line):
    return line.strip() + ' line'

for line in fileinput.input(inplace=True):
    print(process(line))
# Add timestamps to each log line
from datetime import datetime
import fileinput
for line in fileinput.input():
    print(f'[{datetime.now()}] {line}', end='')

These snippets demonstrate how fileinput can be used for simple line‑by‑line processing, in‑place file modification, and real‑time log augmentation, making it a versatile tool for command‑line Python scripts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

file-handlingcommand-linefileinput
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.