On this page
fileinput.lineno
Prev: fileinput.isstdin | Up: fileinput | Next: fileinput.nextfile
Rel: fileinput.close, fileinput.FileInput, fileinput.filelineno, fileinput.filename
Ref: docs.python.org — fileinput.lineno
Tags: #public #python3 #stdlib #fileinput
Import
import fileinput
from fileinput import lineno
Signature & Overview
fileinput.lineno()
Return the cumulative line number of the line that has just been read.
Usage
import fileinput
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p1, p2 = Path(tmp) / "1.txt", Path(tmp) / "2.txt"
p1.write_text("a\n"); p2.write_text("b\n")
with fileinput.input(files=(p1, p2), encoding="utf-8") as f:
nums = [f.lineno() for _ in f]
assert nums == [1, 2]
Gotchas
- Cumulative 1-based line count across ALL files: Use
filelineno()for per-file line numbers.
REPL Inspect
from fileinput import lineno
help(lineno)
Linked from
Related notes
- tuts/python3/stdlib_module/fileinput/isstdin Linked here; links to this page; shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/nextfile Linked here; links to this page; shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/close Linked here; shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/FileInput Linked here; shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/filelineno Linked here; shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/filename Linked here; shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fileinput/fileno shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/hook_compressed shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/hook_encoded shared tags: #fileinput, #stdlib