هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
Catch exceptions
try runs code; except handles a specific error; else runs when there was none; finally always runs.
import json
def load_config(path: str) -> dict:
try:
with open(path, encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
print(f"{path} not found, using defaults")
return {}
except json.JSONDecodeError as e:
raise SystemExit(f"{path} is not valid JSON: line {e.lineno}") from e
finally:
print("config step done")انتبه
Never write a bare except: or except Exception: pass — it hides real bugs. Catch only what you can handle.
Raise your own errors
Stop early with a clear message when input is wrong.
def set_replicas(n: int) -> None:
if not 1 <= n <= 20:
raise ValueError(f"replicas must be 1-20, got {n}")
print("ok", n)Write logs with logging
The logging module adds time, level and source, and can write to a file and the screen at once.
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(name)s: %(message)s",
handlers=[logging.FileHandler("/var/log/backup.log"), logging.StreamHandler()],
)
log = logging.getLogger("backup")
log.info("backup started")
log.warning("disk above %d%%", 80)
try:
1 / 0
except ZeroDivisionError:
log.exception("calculation failed")| Level | Use |
|---|---|
| DEBUG | Details for finding bugs |
| INFO | Normal steps |
| WARNING | Something unusual, still working |
| ERROR | A step failed |
| CRITICAL | The script can not go on |
تذكّر
log.exception(...) inside except also writes the full traceback.
Log to the journal
Scripts run by systemd can simply log to stdout/stderr — the journal stores it. Or send to syslog.
import logging
import logging.handlers
log = logging.getLogger("diskcheck")
log.setLevel(logging.INFO)
log.addHandler(logging.handlers.SysLogHandler(address="/dev/log"))
log.info("diskcheck: / is at 71%")journalctl -t python3 -n 5
journalctl -u diskcheck.service -n 20Rotate log files
A long-running script rotates its own log, or leaves it to logrotate.
import logging
from logging.handlers import RotatingFileHandler
h = RotatingFileHandler("/var/log/app/app.log", maxBytes=5_000_000, backupCount=5)
logging.getLogger().addHandler(h)