fileinput provides one iterator over multiple files and, optionally, standard input. While iterating, code can inspect the current filename, line number, and whether a line starts a new file.
Read files as one stream
import fileinput
for line in fileinput.input(files=("app.log", "worker.log"), encoding="utf-8"):
source = fileinput.filename()
number = fileinput.filelineno()
print(source, number, line.rstrip())
Handle stdin and encoding
A hyphen denotes standard input. Set encoding and errors explicitly instead of inheriting a machine-specific default. Use the object as a context manager when exceptions must not leave handles open.
Treat inplace mode carefully
inplace=True redirects standard output to the file being processed. An exception or stray print can leave partial output. Set backup, test on copies, and prefer an explicit temporary-file replacement for critical data.
Keep learning
Continue with file handling in Python. The official Python documentation, accessed July 22, 2026, covers global state, open hooks, and in-place editing.