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)
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.
fileinput
module:To use the fileinput
module, you need to import it first.
import fileinput
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.
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())
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.
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.
Reading multiple files line by line in Python:
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())
How to use fileinput.input() for reading files:
fileinput.input()
function simplifies reading lines from multiple files by providing a unified input source.import fileinput file_paths = ["file1.txt", "file2.txt", "file3.txt"] for line in fileinput.input(files=file_paths): print(line.strip())
Iterating over lines from multiple files with fileinput:
fileinput
module allows iterating over lines from multiple files seamlessly.import fileinput file_paths = ["file1.txt", "file2.txt", "file3.txt"] for line in fileinput.input(files=file_paths): print(line.strip())
Specifying files to process with fileinput module in Python:
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())
Handling stdin and command-line arguments with fileinput:
fileinput
can handle both standard input (stdin) and command-line arguments, providing flexibility.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())
Fileinput and in-place editing of files in Python:
fileinput
with the inplace
parameter for in-place editing of files.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="")
Customizing behavior with fileinput hooks in Python:
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="")
Fileinput module and line numbering in Python:
fileinput
automatically provides line numbering, which can be accessed using fileinput.lineno()
.import fileinput for line in fileinput.input(files=("file1.txt", "file2.txt")): print(f"Line {fileinput.lineno()}: {line.strip()}")
Using fileinput with regular expressions in Python:
fileinput
.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())