$ run as a normal user# run as root (or with sudo)
Work with paths
Path objects join paths safely and have methods for everything.
paths.py
from pathlib import Path
home = Path.home()
logs = Path("/var/log")
conf = Path("/etc") / "ssh" / "sshd_config"
print(conf.name, conf.suffix, conf.parent, conf.exists(), conf.is_file())
for p in logs.glob("*.log"):
print(p, p.stat().st_size)
for p in Path("/etc").rglob("*.repo"):
print(p)Read and write text files
with open(...) closes the file by itself, also after an error.
read_write.py
from pathlib import Path
with open("/etc/hosts", encoding="utf-8") as f:
for line in f:
if line.strip() and not line.startswith("#"):
print(line.rstrip())
report = Path("/tmp/report.txt")
report.write_text("disk OK\n", encoding="utf-8")
with report.open("a", encoding="utf-8") as f:
f.write("memory OK\n")
print(report.read_text(encoding="utf-8"))| Mode | Meaning |
|---|---|
"r" | Read (default) |
"w" | Write — empties the file first |
"a" | Append to the end |
"x" | Create; fail if it exists |
"rb", "wb" | Binary |
Copy, move and delete
shutil does the file work of cp, mv and rm -r.
organise.py
import shutil
from pathlib import Path
src = Path("/srv/www")
backup = Path("/backup/www-copy")
shutil.copytree(src, backup, dirs_exist_ok=True)
shutil.copy2("/etc/hosts", "/backup/hosts")
shutil.move("/tmp/report.txt", "/backup/report.txt")
shutil.rmtree("/tmp/old-build", ignore_errors=True)
Path("/backup/logs").mkdir(parents=True, exist_ok=True)Watch out
shutil.rmtree deletes without asking. Print the path first, and never build it from user input without checks.
Clean up old files
Find files older than N days and delete (or archive) them.
cleanup.py
import time
from pathlib import Path
days = 14
cutoff = time.time() - days * 86400
for p in Path("/var/tmp/app").rglob("*"):
if p.is_file() and p.stat().st_mtime < cutoff:
print("deleting", p)
p.unlink()Make archives
shutil.make_archive and tarfile pack folders like tar.
archive.py
import shutil
import tarfile
from datetime import date
name = shutil.make_archive(f"/backup/etc-{date.today()}", "gztar", root_dir="/etc")
print(name)
with tarfile.open(name) as t:
print(len(t.getnames()), "files in the archive")