נבנה על ידיBuilt by Ariel Yohimovich

PythonPython
28
חלקPart 5: בונים דבריםBuilding things כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

אוטומציה של משימותAutomating tasks

קבצים ותיקיות בכמויות, תזמון, ודוחות אוטומטיים.

Files and folders in bulk, scheduling, and automatic reports.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

1. למה להתחיל ב-dry run?Why start with a dry run?

2. איזה שם קובץ נשאר בסדר הנכון במיון?Which file name keeps the right order when sorted?

3. מה הכי חשוב בסקריפט שרץ מ-cron?What matters most in a script run by cron?

תרגיליםExercises

תרגילExercise 1

ספירה לפי סוגCounting by type

הקוד יוצר תיקייה עם קבצים. כתבו count_kinds(folder) שמחזירה מילון סיומת (באותיות קטנות, בלי נקודה) → כמות, וקובץ בלי סיומת נספר תחת "none".

The code creates a folder of files. Write count_kinds(folder) returning a dict extension (lowercase, no dot) → count, with extensionless files counted under "none".

איך הפלט אמור להיראות?What should the output look like?
{'none': 1, 'png': 2, 'txt': 2}
main.py
רמזHint 1: p.suffix.lower().lstrip(".") or "none"p.suffix.lower().lstrip(".") or "none"
רמזHint 2: רק קבצים: if p.is_file().Files only: if p.is_file().
לפתרוןSolution
תרגילExercise 2

סידור עם dry runTidying with a dry run

כתבו plan(folder, rules) שמחזירה רשימת זוגות (שם קובץ, תיקיית יעד) בלי להזיז כלום, ו-apply(folder, rules) שמבצעת את התוכנית ומחזירה כמה קבצים הוזזו. rules הוא מילון סיומת → תיקייה, וכל השאר הולך ל-other.

Write plan(folder, rules) returning pairs (file name, target folder) without moving anything, and apply(folder, rules) carrying out the plan and returning how many files moved. rules is a dict extension → folder, and anything else goes to other.

איך הפלט אמור להיראות?What should the output look like?
[('a.pdf', 'docs'), ('b.mp3', 'other'), ('c.PDF', 'docs')]
3 ['docs/a.pdf', 'docs/c.PDF', 'other/b.mp3']
main.py
רמזHint 1: apply משתמשת ב-plan, כך שהן תמיד מסכימות.apply uses plan, so they always agree.
רמזHint 2: shutil.move(str(src), str(dst))shutil.move(str(src), str(dst))
לפתרוןSolution
תרגילExercise 3

מספור תמונותNumbering photos

כתבו new_names(names, prefix) שמקבלת רשימת שמות קבצים, ממיינת אותם, ומחזירה מילון שם ישן → שם חדש בצורה prefix-001.ext (סיומת באותיות קטנות, ו-.jpeg הופך ל-.jpg). בלי לגעת בקבצים.

Write new_names(names, prefix) that takes file names, sorts them, and returns a dict old name → new name in the form prefix-001.ext (lowercase extension, .jpeg becomes .jpg). Without touching files.

איך הפלט אמור להיראות?What should the output look like?
{'IMG_1.jpg': 'trip-001.jpg', 'IMG_10.png': 'trip-002.png', 'IMG_9.JPEG': 'trip-003.jpg'}
main.py
רמזHint 1: Path(n).suffix.lower()Path(n).suffix.lower()
רמזHint 2: f"{prefix}-{i:03}{ext}"f"{prefix}-{i:03}{ext}"
לפתרוןSolution
תרגילExercise 4

שמירה רק של האחרוניםKeeping only the latest

כתבו prune(folder, keep) שמוחקת את קבצי backup-YYYY-MM-DD.zip הישנים ומשאירה רק את keep החדשים ביותר (לפי התאריך בשם). מחזירה רשימת שמות שנמחקו, ממוינת. קבצים אחרים בתיקייה לא נוגעים.

Write prune(folder, keep) deleting old backup-YYYY-MM-DD.zip files and keeping only the keep newest (by the date in the name). Return a sorted list of deleted names. Other files in the folder are untouched.

איך הפלט אמור להיראות?What should the output look like?
['backup-2026-02-27.zip', 'backup-2026-03-01.zip']
['backup-2026-03-02.zip', 'backup-2026-03-03.zip', 'notes.txt']
main.py
רמזHint 1: sorted(Path(folder).glob("backup-*.zip")) ממיין לפי תאריך בזכות הפורמט.sorted(Path(folder).glob("backup-*.zip")) sorts by date thanks to the format.
רמזHint 2: old = files[:-keep] if keep else filesold = files[:-keep] if keep else files
לפתרוןSolution
תרגילExercise 5

מדף, צעד 28: גיבוי ודוח שבועיShelf, step 28: a backup and a weekly report

פרויקט מלווהCompanion project

כתבו daily(folder, day) שיוצרת ZIP של קבצי ה-JSON בתיקייה (folder/backups/shelf-<day>.zip, רק *.json), ו-weekly(log, day) שמקבלת רשימת {day, pages} ומחזירה דוח טקסט: שורה ראשונה Week ending <day>, אחריה Pages: N ו-Best day: <day> (N).

Write daily(folder, day) creating a ZIP of the folder's JSON files (folder/backups/shelf-<day>.zip, only *.json), and weekly(log, day) taking a list of {day, pages} and returning a text report: first line Week ending <day>, then Pages: N and Best day: <day> (N).

איך הפלט אמור להיראות?What should the output look like?
shelf-2026-03-07.zip ['goals.json', 'shelf.json']
Week ending 2026-03-07
Pages: 145
Best day: Wed (75)
automate.py
רמזHint 1: zipfile.ZipFile(path, "w") ו-z.write(f, arcname=f.name) לכל JSON.zipfile.ZipFile(path, "w") and z.write(f, arcname=f.name) for each JSON.
רמזHint 2: best = max(log, key=lambda e: e["pages"])best = max(log, key=lambda e: e["pages"])
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

מוצא כפילויותA duplicate finder

כתבו duplicates(folder) שמוצאת קבצים עם תוכן זהה (גם בתת-תיקיות, גם בשמות שונים) בעזרת hash (hashlib.sha256 על התוכן). מחזירה רשימת קבוצות: כל קבוצה רשימה ממוינת של נתיבים יחסיים (כמחרוזות עם /), רק קבוצות של 2 ומעלה, והקבוצות ממוינות.

Write duplicates(folder) finding files with identical content (in subfolders too, under different names) using a hash (hashlib.sha256 of the content). Return a list of groups: each a sorted list of relative paths (as strings with /), only groups of 2+, and the groups sorted.

איך הפלט אמור להיראות?What should the output look like?
[['2025/c2.jpg', 'c.jpg'], ['2025/copy of a.jpg', 'a.jpg']]
main.py
רמזHint 1: hashlib.sha256(p.read_bytes()).hexdigest() כמפתח במילון.hashlib.sha256(p.read_bytes()).hexdigest() as a dict key.
רמזHint 2: p.relative_to(folder).as_posix() לנתיב עם /.p.relative_to(folder).as_posix() for a path with /.
לפתרוןSolution
אתגרChallenge 2

מפרש cronA cron interpreter

כתבו matches(expr, when) שבודקת אם datetime מתאים לביטוי cron של חמישה שדות (דקה, שעה, יום בחודש, חודש, יום בשבוע כש-0 = ראשון). כל שדה יכול להיות *, מספר, רשימה (1,15), או */n (כל n). ואז next_match(expr, start) שמחזירה את הדקה הראשונה מ-start ואילך שמתאימה (חיפוש דקה אחרי דקה, עד שבוע קדימה).

Write matches(expr, when) checking whether a datetime fits a five-field cron expression (minute, hour, day of month, month, weekday with 0 = Sunday). Each field may be *, a number, a list (1,15), or */n (every n). Then next_match(expr, start) returning the first minute from start onward that matches (searching minute by minute, up to a week ahead).

איך הפלט אמור להיראות?What should the output look like?
2026-03-05 21:30:00
2026-03-08 09:00:00
2026-03-04 10:15:00
main.py
רמזHint 1: יום בשבוע בפייתון: weekday() נותן 0 לשני, אז (when.weekday() + 1) % 7.Python's weekday: weekday() gives 0 for Monday, so (when.weekday() + 1) % 7.
רמזHint 2: פונקציה קטנה field_ok(text, value).A small field_ok(text, value) helper.
לפתרוןSolution

נבנה על ידיBuilt by Ariel Yohimovich