Python Glossary Русский

all Python 3fileinputfunction

fileinput.lineno()

reading state

Returns the cumulative number of the last line read across all files of fileinput.input().

Syntax

fileinput.lineno() -> int

The count does not restart at file boundaries — filelineno() gives the number within a file. Before the first line it returns 0, after the last one the number of that last line. Lines skipped with nextfile() are not counted. Numbering starts at 1, unlike enumerate() by default.

Examples

import fileinput
open('a.txt', 'w', encoding='utf-8').write('a1\na2\n')
open('b.txt', 'w', encoding='utf-8').write('b1\nb2\n')
with fileinput.input(['a.txt', 'b.txt'], encoding='utf-8') as f:
    print(fileinput.lineno())  # → 0
    for line in f:
        pass
    print(fileinput.lineno(), fileinput.filelineno())  # → 4 2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy