Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 3Naam in de cursus: Files and folders

Read, write and organise files and folders

Scripts read logs and configs, write reports, and clean up folders. pathlib handles paths, open() reads and writes, shutil copies, moves and archives.

  • 5 Lessen
  • 0 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

3.1

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)

3.2

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"))
ModeMeaning
"r"Read (default)
"w"Write — empties the file first
"a"Append to the end
"x"Create; fail if it exists
"rb", "wb"Binary

3.3

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)

Let op

shutil.rmtree deletes without asking. Print the path first, and never build it from user input without checks.

3.4

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()

3.5

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")