الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

الفصل 3الاسم في الدورة: 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 الدروس
  • 0 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع 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)

انتبه

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