fileinput.hook_compressed
Prev: fileinput.fileno | Up: fileinput | Next: fileinput.hook_encoded
Rel: fileinput.close, fileinput.FileInput, fileinput.filelineno, fileinput.filename
Ref: docs.python.org — fileinput.hook_compressed
Tags: #public #python3 #stdlib #fileinput
Import
import fileinput
from fileinput import hook_compressed
Signature & Overview
fileinput.hook_compressed(filename, mode, *, encoding=None, errors=None)
fileinput.hook_compressed() function.
Usage
import fileinput
import gzip
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "a.txt.gz"
with gzip.open(p, "wt", encoding="utf-8") as gz:
gz.write("from-gz\n")
with fileinput.input(files=(p,), openhook=fileinput.hook_compressed) as f:
assert next(f).strip() in ("from-gz", b"from-gz")
Gotchas
- Supports
.gzand.bz2transparently: Passencoding="utf-8"(Python 3.10+) for text mode.
REPL Inspect
from fileinput import hook_compressed
help(hook_compressed)
Linked from
Related notes
- tuts/python3/stdlib_module/fileinput/fileno Linked here; links to this page; shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/hook_encoded 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/input shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/isfirstline shared tags: #fileinput, #stdlib
- tuts/python3/stdlib_module/fileinput/isstdin shared tags: #fileinput, #stdlib