On this page
fileinput.input
Prev: fileinput.hook_encoded | Up: fileinput | Next: fileinput.isfirstline
Rel: fileinput.close, fileinput.FileInput, fileinput.filelineno, fileinput.filename
Ref: docs.python.org — fileinput.input
Tags: #public #python3 #stdlib #fileinput
Import
import fileinput
from fileinput import input
Signature & Overview
fileinput.input(files=None, inplace=False, backup='', *, mode='r', openhook=None, encoding=None, errors=None)
Return an instance of the FileInput class, which can be iterated.
Usage
import fileinput
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "a.txt"
p.write_text("ok\n", encoding="utf-8")
with fileinput.input(files=(p,), encoding="utf-8") as f:
assert list(f) == ["ok\n"]
Gotchas
- Always pass
files=when usingargparse: Otherwisefileinput.input()readssys.argv[1:].
REPL Inspect
from fileinput import input
help(input)
Linked from
Related notes
- tuts/python3/stdlib_module/fileinput/hook_encoded Linked here; links to this page; shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/isfirstline 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/isstdin shared tags: #fileinput, #stdlib