Filesystem and I/O Modules
fileinput
Iterates lines from multiple input files as one stream. FileInput supports cumulative line numbers, the current filename and per-file line numbers. Supply explicit filenames rather than relying on command-line arguments; use a context manager and choose an encoding.
Formal Syntax & Structure
python syntaxpython
import fileinputReturn Value
module
Module namespace available through import; import is a statement, not a value-returning expression.
Working Example
Example Snippetpython
from fileinput import FileInputfrom pathlib import Pathfrom tempfile import TemporaryDirectorywith TemporaryDirectory() as folder: a, b = Path(folder)/"a", Path(folder)/"b" a.write_text("red\n", encoding="utf-8") b.write_text("blue\n", encoding="utf-8") with FileInput(files=[a, b], encoding="utf-8") as lines: for line in lines: print(lines.lineno(), line.strip())