On this page
fileinput.nextfile
Prev: fileinput.lineno | Up: fileinput
Rel: fileinput.close, fileinput.FileInput, fileinput.filelineno, fileinput.filename
Ref: docs.python.org — fileinput.nextfile
Tags: #public #python3 #stdlib #fileinput
Import
import fileinput
from fileinput import nextfile
Signature & Overview
fileinput.nextfile()
Close the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count. The filename is not
Usage
import fileinput
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p1, p2 = Path(tmp) / "1.txt", Path(tmp) / "2.txt"
p1.write_text("a1\na2\n"); p2.write_text("b1\n")
firsts = []
with fileinput.input(files=(p1, p2), encoding="utf-8") as f:
for line in f:
firsts.append(line.strip())
f.nextfile()
assert firsts == ["a1", "b1"]
Gotchas
- Skips the remainder of the current file so the next iteration jumps straight to the next file.
REPL Inspect
from fileinput import nextfile
help(nextfile)
Linked from
Related notes
- tuts/python3/stdlib_module/fileinput/lineno 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
- tuts/python3/stdlib_module/fileinput/input shared tags: #fileinput, #stdlib