Python Glossary Русский

all Python 3fileinputfunction

fileinput.isfirstline()

reading state

Returns True if the line just read is the first line of its file.

Syntax

fileinput.isfirstline() -> bool

A handy way to treat the header of each file — for instance, to skip the CSV header in every file, not just once in the combined stream. Note that True refers to the last line READ, so check it inside the loop after getting the line. An empty file produces no first line at all.

Examples

import fileinput
open('a.csv', 'w', encoding='utf-8').write('name\nAnn\n')
open('b.csv', 'w', encoding='utf-8').write('name\nBob\n')
with fileinput.input(['a.csv', 'b.csv'], encoding='utf-8') as f:
    print([line.strip() for line in f if not fileinput.isfirstline()])  # → ['Ann', 'Bob']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy