Python Glossary Русский

all Python 3fileinputfunction

fileinput.filelineno()

reading state

Returns the number of the last line read within the current file of fileinput.input().

Syntax

fileinput.filelineno() -> int

It resets to 1 at the start of every file, while lineno() keeps counting across files; error messages of the form "file:line" need filelineno(). Before the first line it is 0. Checking for the first line is simpler with isfirstline() than with filelineno() == 1.

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:
    for line in f:
        print(fileinput.lineno(), fileinput.filelineno())  # → 1 1 / 2 2 / 3 1 / 4 2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy