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
תרגילים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().
כתבו 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?
כתבו 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?
כתבו 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?
רמז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
מדף, צעד 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?
כתבו 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 /.
כתבו 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?