AzLearn

تمرين إعادة البناء

مسجل مقتطفات آمن

سجل مقتطفات نصية من stdin في ملف محلي بدون مراقبة مستمرة.

python ~15 دقيقة مبتدئ
أعد بناء الكود Rebuild

هذا هو الكود. اكتبه بنفسك.

الكود المرجعي
import argparse
from datetime import datetime
from pathlib import Path
import sys


def append_entry(path: Path, text: str) -> None:
    stamp = datetime.now().isoformat(timespec="seconds")
    with path.open("a", encoding="utf-8") as file:
        file.write(f"[{stamp}] {text.strip()}\n")


def main() -> None:
    parser = argparse.ArgumentParser(description="Append stdin text to a local log file.")
    parser.add_argument("--file", type=Path, default=Path("clipboard-log.txt"))
    args = parser.parse_args()

    text = sys.stdin.read()
    if not text.strip():
        raise SystemExit("Pipe or type text to log.")
    append_entry(args.file, text)
    print(f"Logged entry to {args.file}")


if __name__ == "__main__":
    main()
اكتب هنا