On this page
fileinput.isfirstline
Prev: fileinput.input | Up: fileinput | Next: fileinput.isstdin
Rel: fileinput.close, fileinput.FileInput, fileinput.filelineno, fileinput.filename
Ref: docs.python.org — fileinput.isfirstline
Tags: #public #python3 #stdlib #fileinput
Import
import fileinput
from fileinput import isfirstline
Signature & Overview
fileinput.isfirstline()
Returns true the line just read is the first line of its file, otherwise returns false.
Usage
import fileinput
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "a.txt"
p.write_text("header\nrow1\n", encoding="utf-8")
with fileinput.input(files=(p,), encoding="utf-8") as f:
flags = [f.isfirstline() for _ in f]
assert flags == [True, False]
Gotchas
- Handy for skipping CSV/table header lines across multiple files.
REPL Inspect
from fileinput import isfirstline
help(isfirstline)
Linked from
Related notes
- tuts/python3/stdlib_module/fileinput/input Linked here; links to this page; shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/isstdin 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