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

חזרה למפגשBack
PythonPython

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

מפגש 28: קבצים ותיקיות בכמויות, תזמון, ודוחות אוטומטיים.Session 28: Files and folders in bulk, scheduling, and automatic reports.

מה נעשה היוםToday's plan

  • למהWhy
  • סידורTidying
  • שמותNames
  • גיבויBackup
  • דוחReport
  • תזמוןScheduling
נושאTopic 1

למהWhy

מה כדאי לאוטמט?What's worth automating?

  • משהו שעושים שוב ושובSomething you do again and again
  • כללים ברורים, בלי שיקול דעתClear rules, no judgement
  • טעות אנושית יקרהHuman mistakes are costly

אנלוגיהAnalogy

אוטומציה היא כמו מדיח כלים: צריך ללמוד פעם אחת איך להעמיס אותו, ואז הוא חוסך לכם עבודה כל ערב.Automation is like a dishwasher: learn once how to load it, and it saves you work every evening.
נושאTopic 2

סידורTidying

לסדר תיקייהTidying a folder

  • מילון סיומת → תיקייהA dict extension → folder
  • shutil.move(src, dst)shutil.move(src, dst)
  • הרצת ניסיון קודם!A dry run first!

אנלוגיהAnalogy

הרצת ניסיון היא כמו לסמן בעיפרון לפני שקודחים בקיר: אם הסימון במקום הלא נכון, מוחקים בלי חור.A dry run is like marking with a pencil before drilling the wall: if the mark's in the wrong place, you erase it, no hole.
נושאTopic 3

שמותNames

שינוי שמות בכמותRenaming in bulk

  • p.stem, p.suffix, p.with_name(...)p.stem, p.suffix, p.with_name(...)
  • מספור עם אפסים: f"{i:03}"Numbering with zeros: f"{i:03}"
  • ממיינים לפני שממספריםSort before numbering

אנלוגיהAnalogy

אפסים מובילים הם כמו מספרי מושבים באולם: 007 תמיד לפני 010, גם כשמסדרים לפי האלף-בית.Leading zeros are like seat numbers in a hall: 007 always comes before 010, even when sorted alphabetically.
נושאTopic 4

גיבויBackup

גיבוי מתוארךA dated backup

  • חותמת זמן בשם הקובץA timestamp in the file name
  • shutil.make_archive יוצר ZIPshutil.make_archive makes a ZIP
  • שומרים רק את N האחרוניםKeep only the last N

אנלוגיהAnalogy

גיבוי עם תאריך הוא כמו תמונות בגלריה עם תאריך: יודעים בדיוק איך הדברים נראו ביום מסוים, ולא מתבלבלים בין "final" ל-"final2".A dated backup is like dated photos in a gallery: you know exactly how things looked on a given day, no mixing up "final" and "final2".
נושאTopic 5

דוחReport

דוח אוטומטיAn automatic report

  • אוספים נתונים מתיקייהCollect data from a folder
  • כותבים Markdown/טקסטWrite Markdown/text
  • שמים בתיקיית reports/ עם תאריךPut it in reports/ with a date

אנלוגיהAnalogy

דוח אוטומטי הוא כמו דף סיכום שמישהו מניח לכם על השולחן כל יום ראשון: אתם רק קוראים.An automatic report is like a summary sheet someone leaves on your desk every Sunday: you just read it.
נושאTopic 6

תזמוןScheduling

להריץ לבד בזמן קבועRunning on its own, on time

  • Mac/Linux: cron (crontab -e)Mac/Linux: cron (crontab -e)
  • Windows: Task SchedulerWindows: Task Scheduler
  • נתיבים מלאים + לוג לקובץFull paths + a log file

אנלוגיהAnalogy

cron הוא כמו שעון מעורר למחשב: מכוונים פעם אחת, והוא מצלצל לבד בכל יום, גם אם שכחתם.cron is like an alarm clock for the computer: set it once, and it rings every day by itself, even if you forget.

טעות נפוצהCommon mistake

backup.py
import os
from pathlib import Path
os.chdir("/")
print(Path("shelf.json").read_text())
מה Python עונהWhat Python says
FileNotFoundError: [Errno 2] No such file or directory: 'shelf.json'
FileNotFoundError: [Errno 2] No such file or directory: 'shelf.json': cron מריץ מתיקייה אחרת. בונים נתיבים יחסית לסקריפט: Path(__file__).parent / "shelf.json".FileNotFoundError: [Errno 2] No such file or directory: 'shelf.json': cron runs from a different folder. Build paths relative to the script: Path(__file__).parent / "shelf.json".

משהו שעושים שוב ושובSomething you do again and again

כללים ברורים, בלי שיקול דעתClear rules, no judgement

טעות אנושית יקרהHuman mistakes are costly

מילון סיומת → תיקייהA dict extension → folder

shutil.move(src, dst)shutil.move(src, dst)

הרצת ניסיון קודם!A dry run first!

p.stem, p.suffix, p.with_name(...)p.stem, p.suffix, p.with_name(...)

מספור עם אפסים: f"{i:03}"Numbering with zeros: f"{i:03}"

ממיינים לפני שממספריםSort before numbering

חותמת זמן בשם הקובץA timestamp in the file name

shutil.make_archive יוצר ZIPshutil.make_archive makes a ZIP

שומרים רק את N האחרוניםKeep only the last N

אוספים נתונים מתיקייהCollect data from a folder

כותבים Markdown/טקסטWrite Markdown/text

שמים בתיקיית reports/ עם תאריךPut it in reports/ with a date

Mac/Linux: cron (crontab -e)Mac/Linux: cron (crontab -e)

Windows: Task SchedulerWindows: Task Scheduler

מה למדנוWhat we learned

  • משהו שעושים שוב ושובSomething you do again and again
  • מילון סיומת → תיקייהA dict extension → folder
  • p.stem, p.suffix, p.with_name(...)p.stem, p.suffix, p.with_name(...)
  • חותמת זמן בשם הקובץA timestamp in the file name
  • אוספים נתונים מתיקייהCollect data from a folder
  • Mac/Linux: cron (crontab -e)Mac/Linux: cron (crontab -e)

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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