Python Glossary Русский

all Python 3fileinputfunction

fileinput.nextfile()

reading state

Closes the current file so that the next iteration starts with the next file; unread lines are skipped.

Syntax

fileinput.nextfile()

Skipped lines are not counted by lineno(), so the numbers will not match "line number in the concatenated text". Before the first line has been read nextfile() does nothing — it cannot skip the first file entirely. The name in filename() changes only after the first line of the next file has been read.

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:
        if fileinput.filename() == 'a.txt':
            fileinput.nextfile()  # заголовка хватит
        print(fileinput.lineno(), line.strip())  # → 1 a1 / 2 b1 / 3 b2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy