Files#
pathlib and with open snippets the operator reaches for
when scanning a host, parsing a dump, or stitching pipeline
artefacts together.
Read a whole text file.
from pathlib import Path
text = Path("notes.txt").read_text(encoding="utf-8")
Read a whole binary file.
data = Path("dump.bin").read_bytes()
Write text in one call.
Path("/tmp/out.txt").write_text("hello\n", encoding="utf-8")
Write bytes in one call.
Path("/tmp/dump.bin").write_bytes(payload)
Stream a large file line-by-line; iterating the file object is constant-memory.
with open("/var/log/auth.log", encoding="utf-8") as f:
for line in f:
if "Failed password" in line:
print(line.strip())
Tail the last N lines without loading the file.
from collections import deque
with open("/var/log/syslog") as f:
last = deque(f, maxlen=100)
Glob for files with a pattern.
for cfg in Path("/etc").glob("*.conf"):
print(cfg.name, cfg.stat().st_size)
Recursive glob.
for py in Path("src").rglob("*.py"):
audit(py)
Walk a directory tree.
import os
for root, dirs, files in os.walk("/var/log"):
for name in files:
handle(Path(root) / name)
Hash a file in fixed-size chunks.
import hashlib
h = hashlib.sha256()
with open("payload.bin", "rb") as f:
for chunk in iter(lambda: f.read(65536), b""):
h.update(chunk)
digest = h.hexdigest()
Read a fixed-size header then jump to an offset.
with open("dump.bin", "rb") as f:
header = f.read(16)
f.seek(0x100)
payload = f.read()
Create a temporary directory that cleans itself up.
import tempfile
with tempfile.TemporaryDirectory() as d:
work(Path(d))