Vibe Coding: build real apps with AI · Episode 24. Every agent run below is real: Claude Code 2.1.322 (model claude-opus-5-5) in print mode (claude -p --permission-mode acceptEdits), on 12 November 2026, in /home/asha/projects/trip-organiser (a Git repository). The agent could not run commands without approval, so it could not test its own scripts; we ran and tested everything, and every output below is copied from a real run.
What's in the project folder: 312 small JPEG photos made for this video (140 named like Asha's Android phone, IMG_20261030_174209.jpg, with the date saved inside; 120 named like Mummy's iPhone, IMG_4825.JPG, date saved inside; 52 named like WhatsApp images, IMG-20261030-WA0001.jpg, with no saved date), and trip_expenses.csv, a 22-row expense sheet saved the way Excel saves "CSV UTF-8" files (with a hidden BOM at the start and Windows line endings). Python 3.13, Ubuntu 24.04, cron 3.0pl1.
Safety in this episode: no real email account is used. The app password on screen is fake (fake pass word demo). The "Gmail" server in the attack test is a fake server we ran on the same laptop; smtp.gmail.com was pointed at it in /etc/hosts only for the test and removed afterwards. Never put a real password on screen, in a script, or in a Git repository.
The problem: 312 photos from three phones
After a four-day trip to Jaipur (30 October to 2 November 2026), Asha's family has 312 photos from three sources in one folder. Sorted by name, the folder is out of time order:
$ ls photos
IMG-20261030-WA0001.jpg <- file 1
...
IMG-20261102-WA0013.jpg <- file 52
IMG_20261030_081353.jpg <- file 53
...
IMG_20261102_205705.jpg <- file 192
IMG_4801.JPG <- file 193
...
IMG_4920.JPG <- file 312
All 52 WhatsApp photos come first (a - sorts before _), then Asha's phone, then Mummy's iPhone. IMG_4801.JPG, the very first photo of the trip (30 Oct, 08:11:18), is file number 193.
Mummy's expense sheet has the usual mess: "₹3,450", " 320 ", food and FOOD, a blank row, the same kachori breakfast entered twice, - as an amount, and dates written as 30/10/2026, 1/11/2026 and 02-11-2026.
Obvious way: do it by hand
Open each photo, check when it was taken, type a new name: about 10 seconds a photo, so 312 × 10 s ≈ 52 minutes, after every trip, with typos. It hurts because it is the same rule applied 312 times, and a rule is exactly what a computer follows perfectly.
What a script is (a recipe card)
| In the kitchen | On the computer |
|---|---|
| A recipe card: steps written down once | A script: a text file of steps, e.g. rename_photos.py |
| The cook follows it exactly, every time | You run it: python3 rename_photos.py (Python reads the file and does the steps) |
| Cook it every Sunday without rewriting it | Automation: the computer runs it on a timetable, even while you sleep |
Python is a programming language that is easy to read; python3 is the program that runs Python files. The standard library is the set of tools that comes with Python (os, csv, smtplib, ssl...), so a script that uses only the standard library needs nothing extra installed.
Files, folders and paths (a cupboard with labelled boxes)
- A file is a labelled box: a name outside (
IMG_4801.JPG), contents inside (the photo's bytes). - A folder (directory) is a shelf that groups files:
photosholds 312 files. - A path is the full address of a file:
/home/asha/projects/trip-organiser/photos/IMG_4801.JPG. - An absolute path starts from the top,
/on Linux and Mac (orC:\on Windows). It means the same place wherever you are. - A relative path like
photosmeans "photosinside the folder I am in now". - The working directory (current directory) is the folder a program starts in. A relative path is read from there. This is the cause of the cron bug later.
Writing and reading a file (run in the compiler)
# Write a file, then read it back
with open("shopping.txt", "w") as f:
f.write("milk\n")
f.write("bread\n")
f.write("chai patti\n")
with open("shopping.txt") as f:
for line in f:
print("Need:", line.strip())
import os
print("Files in this folder:", sorted(os.listdir(".")))
Real output:
Need: milk
Need: bread
Need: chai patti
Files in this folder: ['main.py', 'shopping.txt']
open(name, "w") opens a file for writing (creating it, or emptying it if it exists). open(name) opens it for reading. with ... as f: closes the file for you at the end. "\n" is the newline character, the end of a line; .strip() removes it (and spaces) from the ends. os.listdir(".") lists the files in the current folder (. means "here").
The obvious script, and the photo it lost (run in the compiler)
import os
# Four photos from three phones (empty files, just the names)
taken = { # old name -> when it was taken
"IMG_20261030_093258.jpg": "2026-10-30_09-32-58",
"IMG_20261030_174209.jpg": "2026-10-30_17-42-09",
"IMG_4825.JPG": "2026-10-30_17-42-09", # Mummy, same group photo
"IMG-20261030-WA0001.jpg": "2026-10-30_WA0001",
}
for old in taken:
open(old, "w").close()
print("Before:", len([f for f in os.listdir(".") if f.lower().endswith(".jpg")]), "photos")
# The obvious script: rename each one to its date and time
for old, new in taken.items():
os.rename(old, new + ".jpg")
print(" ", old, "->", new + ".jpg")
photos = sorted(f for f in os.listdir(".") if f.endswith(".jpg"))
print("After: ", len(photos), "photos")
for p in photos:
print(" ", p)
Real output:
Before: 4 photos
IMG_20261030_093258.jpg -> 2026-10-30_09-32-58.jpg
IMG_20261030_174209.jpg -> 2026-10-30_17-42-09.jpg
IMG_4825.JPG -> 2026-10-30_17-42-09.jpg
IMG-20261030-WA0001.jpg -> 2026-10-30_WA0001.jpg
After: 3 photos
2026-10-30_09-32-58.jpg
2026-10-30_17-42-09.jpg
2026-10-30_WA0001.jpg
Why: everyone took the group photo at the same second, so two different photos got the same new name, and on this Linux computer os.rename replaced the existing file without any error or question. Scripts are fast, and they are fast at mistakes too.
The fix: check first, and add _2 if the name is taken.
for old, new in taken.items():
target = new + ".jpg"
n = 2
while os.path.exists(target): # never overwrite: pick a free name
target = f"{new}_{n}.jpg"
n += 1
os.rename(old, target)
print(" ", old, "->", target)
Real output (same four photos):
IMG_20261030_093258.jpg -> 2026-10-30_09-32-58.jpg
IMG_20261030_174209.jpg -> 2026-10-30_17-42-09.jpg
IMG_4825.JPG -> 2026-10-30_17-42-09_2.jpg
IMG-20261030-WA0001.jpg -> 2026-10-30_WA0001.jpg
After: 4 photos
Prompt 1: the AI writes rename_photos.py
claude -p "I have 312 photos from our Jaipur trip in the photos folder: from my phone, Mummy's iPhone and WhatsApp, all mixed together, and the names don't sort in the order they were taken. Write a Python script rename_photos.py that renames every photo to the date and time it was taken, like 2026-10-30_17-42-09.jpg, so they sort in time order. Use only the Python standard library if you can." --permission-mode acceptEdits
What it really did (log shortened): it listed the folder first (allowed), tried to run a small script to read the photos' saved dates ("This command requires approval"), wrote a 275-line rename_photos.py, tried a dry run ("requires approval") and then said plainly that the script was untested on your 312 files. Its plan:
- the date and time saved inside the photo by the camera (EXIF) for both phones;
- the file name, for Asha's phone (
IMG_20261030_081353→2026-10-30_08-13-53); - WhatsApp photos: the name gives only the day; if the file's modified time is on the same day, use it, otherwise name it
2026-10-30_WA0001.jpgso it sorts after that day's photos.
It also built in, unasked: a dry run by default, --apply to really rename, --undo to restore, a log file photos/rename_log.csv, and _2, _3 for photos taken in the same second ("No file is ever overwritten"). It warned that WhatsApp usually removes the saved date, so "no script can fully fix that".
Three safety ideas worth stealing
- EXIF (Exchangeable Image File Format): information a camera saves inside the photo file, such as the date and time it was taken, like the date stamp on the back of an old printed photo. Mummy's iPhone names contain no date, so EXIF is the only way to know when they were taken. Our WhatsApp images had none (the AI expected this: "WhatsApp usually removes the saved date").
- Dry run: a rehearsal. The script prints exactly what it would do and changes nothing, like a fire drill.
- Undo log: a file recording every old name and new name, so every change can be reversed.
- And our own rule: commit the originals to Git before running anything (
11ca123 Trip photos and expenses (originals)).
Testing it (real)
$ python3 rename_photos.py
IMG_4801.JPG -> 2026-10-30_08-11-18.jpg [EXIF]
IMG_20261030_081353.jpg -> 2026-10-30_08-13-53.jpg [EXIF]
IMG_4802.JPG -> 2026-10-30_08-26-22.jpg [EXIF]
...
IMG_20261030_174209.jpg -> 2026-10-30_17-42-09.jpg [EXIF]
IMG_4825.JPG -> 2026-10-30_17-42-09_2.jpg [EXIF]
...
IMG-20261102-WA0013.jpg -> 2026-11-02_WA0013.jpg [WhatsApp date only]
Where the dates came from:
260 EXIF
52 WhatsApp date only
Dry run: nothing changed. 312 files would be renamed. Run again with --apply to rename.
All three same-second pairs (17:42:09 on 30 Oct, 11:05:33 on 31 Oct, 19:20:48 on 1 Nov) got _2. Then:
$ python3 rename_photos.py --apply | tail -1
Renamed 312 files. Original names saved in photos/rename_log.csv (use --undo to restore).
$ ls photos | head -3
2026-10-30_08-11-18.jpg
2026-10-30_08-13-53.jpg
2026-10-30_08-26-22.jpg
$ python3 rename_photos.py --undo | tail -1
Restored 312 original names.
Test the undo before you need it, not after.
Cleaning an Excel CSV
A CSV (comma-separated values) file is a table saved as plain text: one row per line, commas between the columns. Excel can open and save it. Data cleaning means making every value follow one format (amounts as plain numbers, one spelling per category, one date format) and flagging what can't be fixed (duplicates, missing values) for a human to decide.
$ cat trip_expenses.csv
Date,Item,Category,Amount,Paid by
30/10/2026,Train tickets Delhi-Jaipur,Travel,"₹3,450",Papa
30/10/2026,Auto to hotel,Travel,180,Asha
31/10/2026,Lassi,food ,120,Asha
31/10/2026,Dinner at Chokhi Dhani,Food,"2,850",Papa
1/11/2026,Kachori breakfast,FOOD,160,Asha
1/11/2026,Kachori breakfast,FOOD,160,Asha
1/11/2026,Cab to City Palace,Travel, 320 ,Papa
02/11/2026,Train snacks,Food,-,Asha
02-11-2026,Train tickets Jaipur-Delhi,Travel,"₹3,450",Papa
... (22 rows in all)
Notice "₹3,450" is in quotes: the comma inside is part of the amount, not a column separator. Python's csv module understands those quotes; splitting each line on commas yourself would not.
The obvious way, and two real errors (run in the compiler)
import csv
# Three rows of Mummy's sheet, saved the way her file was saved
with open("trip.csv", "w", encoding="utf-8-sig") as f:
f.write('Date,Item,Category,Amount,Paid by\n')
f.write('30/10/2026,Train tickets,Travel,"₹3,450",Papa\n')
f.write('30/10/2026,Auto to hotel,Travel,180,Asha\n')
f.write('31/10/2026,Lassi,food ,120,Asha\n')
# The obvious way: read it and add up the Amount column
total = 0
with open("trip.csv", encoding="utf-8") as f:
for row in csv.DictReader(f):
print(row["Date"], row["Item"], row["Amount"])
total += float(row["Amount"])
print("Total:", total)
Real output:
Traceback (most recent call last):
File "main.py", line 13, in <module>
print(row["Date"], row["Item"], row["Amount"])
~~~^^^^^^^^
KeyError: 'Date'
csv.DictReader turns each row into a dictionary (a set of name → value pairs) using the first line as the column names. A KeyError means "there is no column with exactly that name". Instead of guessing, print the names it found (print(list(row.keys()))):
['\ufeffDate', 'Item', 'Category', 'Amount', 'Paid by']
\ufeff is the BOM (byte order mark): an invisible character at the very start of the file. Mummy's file has one; the AI later told us Excel adds it. Reading with encoding="utf-8-sig" skips it. An encoding is the rule for turning characters (like ₹) into bytes and back; UTF-8 is the common one, and utf-8-sig is UTF-8 that expects and drops a BOM. With that fixed, the next real error:
30/10/2026 Train tickets ₹3,450
Traceback (most recent call last):
File "main.py", line 14, in <module>
total += float(row["Amount"])
~~~~~^^^^^^^^^^^^^^^
ValueError: could not convert string to float: '₹3,450'
float() turns text into a number with decimals, and ₹ and , are not digits. Clean before you count:
def money(text): # "₹3,450" -> 3450.0
return float(text.replace("₹", "").replace(",", "").strip())
total = 0
by_cat = {}
with open("trip.csv", encoding="utf-8-sig") as f: # -sig skips the hidden mark
for row in csv.DictReader(f):
amount = money(row["Amount"])
cat = row["Category"].strip().title() # "food " -> "Food"
total += amount
by_cat[cat] = by_cat.get(cat, 0) + amount
print("Total:", total)
print(by_cat)
Real output:
Total: 3750.0
{'Travel': 3630.0, 'Food': 120.0}
Prompt 2: the AI cleans the whole file
claude -p "Mummy exported our trip expenses from Excel as trip_expenses.csv. Write a Python script clean_expenses.py that cleans it up and tells us how much we spent in total, per category and per person, and saves a clean copy as expenses_clean.csv that opens properly in Excel." --permission-mode acceptEdits
It could not run the script either, so it worked out the expected totals by hand and asked us to check. Our real run:
$ python3 clean_expenses.py
Cleaned 19 expenses -> expenses_clean.csv
TOTAL SPENT: ₹29,060
By category:
Stay ₹9,600 (33.0%)
Travel ₹7,600 (26.2%)
Food ₹4,810 (16.6%)
Shopping ₹4,750 (16.3%)
Tickets ₹2,000 ( 6.9%)
Fun ₹300 ( 1.0%)
By person (who paid):
Papa ₹21,870 (75.3%)
Mummy ₹6,340 (21.8%)
Asha ₹850 ( 2.9%)
Duplicate rows removed - please double-check these were really entered twice:
line 14: Kachori breakfast, ₹160, Asha
(run with --keep-duplicates if they were genuinely separate purchases)
Needs attention:
line 22: 'Train snacks' has no amount ('-') - left out
$ head -3 expenses_clean.csv
Date,Item,Category,Amount,Paid by
2026-10-30,Train tickets Delhi-Jaipur,Travel,3450,Papa
2026-10-30,Auto to hotel,Travel,180,Asha
Good choices worth noticing in its code: dates are read day first ("%d/%m/%Y", "%d-%m-%Y", ...), so 02/11/2026 is 2 November, not February 11; they are written as YYYY-MM-DD so they sort correctly; money is added with Decimal (exact decimal numbers), not float, which can't store values like 0.1 exactly; the clean file is written with a BOM again so Excel shows ₹ correctly. We checked the totals another way (a few lines of our own Python): the same ₹29,060, Papa ₹21,870, Mummy ₹6,340, Asha ₹850.
Scheduling with cron (the milkman)
| The milkman | cron |
|---|---|
| Comes at 6 every morning; you never call him | cron: a program on Linux and Mac that runs jobs on a timetable |
| His delivery list: who, what, when | The crontab (cron table): your list of jobs; edit it with crontab -e, show it with crontab -l |
| "Sundays, extra paneer" | 0 21 * * 0: the five time fields |
Each crontab line is five time fields and then the command. From the crontab(5) manual: minute (0-59), hour (0-23), day of month, month, day of week (0-7, where 0 or 7 is Sunday). * means "every". So:
| Line | Means |
|---|---|
* * * * * |
every minute (we used it only for testing) |
0 21 * * 0 |
minute 0, hour 21 (9 pm), any date, any month, Sunday |
5 4 * * sun |
4:05 am every Sunday (the manual's own example) |
cron uses the computer's own clock and time zone. A cron job has no screen, so send its output to a log file: >> /home/asha/cron.log appends normal output to the file, and 2>&1 sends the errors (stream 2) to the same place as the normal output (stream 1).
The obvious crontab lines fail (real cron.log)
$ crontab -l
* * * * * python3 rename_photos.py --apply >> /home/asha/cron.log 2>&1
$ cat ~/cron.log (one minute later)
python3: can't open file '/home/asha/rename_photos.py': [Errno 2] No such file or directory
$ crontab -l
* * * * * python3 /home/asha/projects/trip-organiser/rename_photos.py --apply >> /home/asha/cron.log 2>&1
$ cat ~/cron.log (one minute later)
Folder not found: photos
Why: cron starts the job in your home folder, with a small environment. The crontab(5) manual: "SHELL is set to /usr/bin/sh" and "LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner." When we tested by hand we were inside the project folder, so the relative paths rename_photos.py and photos worked; under cron they pointed at /home/asha/rename_photos.py and /home/asha/photos. The AI's script had default="photos": a relative path.
Prompt 3: real errors in, a fix out
claude -p "I want the photo renaming to run by itself every Sunday at 9 pm with cron. I tried these two crontab lines and both failed (this is from my cron.log):
1) * * * * * python3 rename_photos.py --apply -> python3: can't open file '/home/asha/rename_photos.py': [Errno 2] No such file or directory
2) * * * * * python3 /home/asha/projects/trip-organiser/rename_photos.py --apply -> Folder not found: photos
Fix it so it works from cron. Also, after each weekly run, email Papa a short summary (how many photos were renamed) from my Gmail. My Gmail address and app password must not be written inside the code. Give me the exact crontab line to use." --permission-mode acceptEdits
Its reply explained both failures, and found a third bug on its own: "There was also a third problem that would have shown up in week 2. After the first run the script refused to go on because rename_log.csv already existed, so every Sunday after that would have failed." Its fix:
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DEFAULT_FOLDER = os.path.join(SCRIPT_DIR, "photos")
MAIL_CONFIG = os.path.expanduser("~/.config/trip-organiser/mail.env")
MAIL_KEYS = ("GMAIL_ADDRESS", "GMAIL_APP_PASSWORD", "EMAIL_TO")
...
ap.add_argument("folder", nargs="?", default=DEFAULT_FOLDER,
help="photo folder (default: the 'photos' folder next to this script)")
__file__ is the path of the running script; os.path.abspath makes it absolute; os.path.dirname takes its folder. So DEFAULT_FOLDER is the photos folder next to the script, wherever cron starts. ~ (tilde) means the user's home folder, and os.path.expanduser turns it into /home/asha. The crontab line it gave:
0 21 * * 0 python3 /home/asha/projects/trip-organiser/rename_photos.py --apply --email >> /home/asha/cron.log 2>&1
and the warning: "Delete the old lines straight away. * * * * * means every minute."
Secrets in a private settings file
$ cat ~/.config/trip-organiser/mail.env (fake values for this video)
GMAIL_ADDRESS=asha@example.com
GMAIL_APP_PASSWORD=fake pass word demo
EMAIL_TO=papa@example.com
$ chmod 600 ~/.config/trip-organiser/mail.env
$ ls -l ~/.config/trip-organiser/mail.env
-rw------- 1 asha asha 96 Nov 12 09:35 /home/asha/.config/trip-organiser/mail.env
The file is outside the project, so Git never sees it. chmod (change mode) sets who may read or write a file: 600 means the owner can read and write, nobody else can do anything (-rw-------). The script also warns if the file can be read by others.
An app password (Google Account Help, "Sign in with app passwords"): "a 16-digit passcode that gives a less secure app or device permission to access your Google Account"; it "can only be used with accounts that have 2-Step Verification turned on" (2-Step Verification: signing in needs your password plus a second proof, like a code on your phone). Google also says app passwords "aren't recommended and are unnecessary in most cases", and you can remove one at any time from the App passwords page, after which "the app can't access your Google Account again". Never use your main password in a script.
Windows: Task Scheduler
Windows has no cron; its built-in scheduler is Task Scheduler, and schtasks is its command-line tool. Microsoft's own weekly example (Microsoft Learn, "schtasks create"):
schtasks /create /tn MyApp /tr c:\apps\myapp.exe /sc weekly /d WED
For Asha's script it would be (untested here: we had no Windows computer):
schtasks /create /tn TripPhotos /sc weekly /d SUN /st 21:00 ^
/tr "<full path to python.exe> C:\Users\asha\trip-organiser\rename_photos.py --apply --email"
/tn is the task name, /sc weekly /d SUN the schedule, /st the start time ("24-hour time format, HH:mm"), and /tr the program to run: "Type the fully qualified path and file name". The same lesson as cron: full paths. (^ continues a long command on the next line in the Windows Command Prompt.)
AI mistake: it handed the password to a fake server
SMTP (Simple Mail Transfer Protocol) is the language computers use to send email; Gmail's sending server is smtp.gmail.com. The AI's email code:
with smtplib.SMTP_SSL("smtp.gmail.com", 465, timeout=60) as smtp:
smtp.login(s["GMAIL_ADDRESS"], password)
smtp.send_message(msg)
It looks secure: SSL is even in the name. We tested it the way an attacker would: what if a bad network (say, dodgy café Wi-Fi) sends you to a fake server calling itself smtp.gmail.com? On our own laptop we pointed that name at a fake mail server we ran ourselves, with a self-signed certificate (an ID card it signed itself), and let cron run the job. Real output:
$ cat ~/cron.log
...
[2026-11-12 09:36] Renamed 312 files. Original names saved in .../photos/rename_log.csv
Summary emailed to papa@example.com.
fake server window:
[fake server] listening on 465 as smtp.gmail.com
[fake server] LOGIN user=asha@example.com password=fakepassworddemo
[fake server] MAIL to ['papa@example.com']
Subject: Trip photos: 312 renamed (12 Nov 2026)
The script logged in to a server that wasn't Google and handed over the password, and reported success.
Why: encrypted is not the same as verified
A certificate is a server's ID card: it says "I am smtp.gmail.com" and is signed by a certificate authority (CA) your computer trusts. TLS (the modern name for SSL) encrypts the connection, and should also check the ID card: is it signed by a trusted authority, and is it for the name we asked for? From Python 3.13's own source code on this laptop:
# smtplib.py: what SMTP_SSL does when you pass no context
if context is None:
context = ssl._create_stdlib_context()
# ssl.py
_create_stdlib_context = _create_unverified_context
With no context, SMTP_SSL encrypts but never checks the certificate. (An SSLContext, "context" for short, is the bundle of TLS settings: which authorities to trust, whether to check names.)
Prompt 4: the fix
claude -p "I tested the email part against a FAKE mail server that pretends to be smtp.gmail.com (I pointed smtp.gmail.com at my own computer in /etc/hosts, with a self-signed certificate). Your script connected, logged in and handed it my app password. The fake server printed:
[fake server] LOGIN user=asha@example.com password=fakepassworddemo
A safe script should have refused to talk to a server whose certificate isn't real. Fix that, and change nothing else." --permission-mode acceptEdits
The real diff:
- with smtplib.SMTP_SSL("smtp.gmail.com", 465, timeout=60) as smtp:
+ # Without an explicit context, SMTP_SSL does NOT check the server's
+ # certificate. create_default_context() verifies it against the system's
+ # trusted CAs and checks the hostname, so an impostor is refused before
+ # login() ever sends the password.
+ tls = ssl.create_default_context()
+ with smtplib.SMTP_SSL("smtp.gmail.com", 465, timeout=60, context=tls) as smtp:
(plus import ssl). Retest, the next cron run against the same fake server:
[2026-11-12 09:40] Nothing new to rename.
Could not send email: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate (_ssl.c:1035)
The fake server logged no LOGIN line: the password never left the laptop. A failed email is fine; a leaked password is not. (This is called failing closed: when a check fails, stop, rather than carry on unsafely.)
Notifications done safely
- Secrets outside the code and outside the project, readable only by you (
chmod 600). Fake values on screen, always. - An app password you can remove at any time, never your main password.
- Verify the server before logging in:
ssl.create_default_context(). - Test by sending to yourself first; send one summary, not one message per photo.
- WhatsApp-style alerts: WhatsApp's Messaging Guidelines say not to "use unofficial clients, bulk messaging, auto-messaging, auto-dialing, or automation to harm WhatsApp or our users", and accounts can be suspended "temporarily or permanently". For phone alerts, use an official bot API, such as the Telegram bot from Episode 18, with its token kept out of the code.
The final setup
$ crontab -l
0 21 * * 0 python3 /home/asha/projects/trip-organiser/rename_photos.py --apply --email >> /home/asha/cron.log 2>&1
$ git log --oneline
64308e7 Check the mail server certificate (Claude Code, prompt 4); photos renamed
4cbc035 Run from cron, weekly log, email summary (Claude Code, prompt 3)
13d9e7c Clean trip expenses (Claude Code, prompt 2)
27957b1 Rename photos by date taken (Claude Code, prompt 1)
11ca123 Trip photos and expenses (originals)
The finished scripts (the AI's real code)
rename_photos.py (after prompts 1, 3 and 4)
#!/usr/bin/env python3
"""Rename photos to the date and time they were taken, e.g. 2026-10-30_17-42-09.jpg
Where the time comes from, best source first:
1. EXIF "DateTimeOriginal" stored inside the JPEG (phone camera photos)
2. The filename, if it contains date AND time (Android: IMG_20261030_174209.jpg)
3. WhatsApp names (IMG-20261030-WA0007.jpg) contain only the date. WhatsApp
removes EXIF, so the time comes from the file's modified time, but only if
that falls on the same day. Otherwise the file becomes 2026-10-30_WA0007.jpg
and sorts after that day's photos.
Usage:
python3 rename_photos.py # dry run: shows the plan, changes nothing
python3 rename_photos.py --apply # actually rename
python3 rename_photos.py --undo # put the original names back
python3 rename_photos.py other_folder --apply
python3 rename_photos.py --apply --email # rename, then email a summary
With no folder given, the "photos" folder next to this script is used, so it
works from any directory (including cron).
Every rename is recorded in rename_log.csv inside the folder (each run adds to
it); --undo uses it and restores the original names from all runs.
--email sends a short summary through Gmail. The login is NOT stored in this
file. It is read from environment variables, or from the file
~/.config/trip-organiser/mail.env (keep it private: chmod 600), containing:
GMAIL_ADDRESS=you@gmail.com
GMAIL_APP_PASSWORD=abcd efgh ijkl mnop
EMAIL_TO=papa@example.com
Only the Python standard library is used.
"""
import argparse
import csv
import os
import re
import smtplib
import socket
import ssl
import stat
import struct
import sys
from collections import Counter
from datetime import datetime
from email.message import EmailMessage
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DEFAULT_FOLDER = os.path.join(SCRIPT_DIR, "photos")
MAIL_CONFIG = os.path.expanduser("~/.config/trip-organiser/mail.env")
MAIL_KEYS = ("GMAIL_ADDRESS", "GMAIL_APP_PASSWORD", "EMAIL_TO")
PHOTO_EXTS = {".jpg", ".jpeg"}
LOG_NAME = "rename_log.csv"
OUT_FMT = "%Y-%m-%d_%H-%M-%S"
ANDROID_RE = re.compile(r"(20\d{2})(\d{2})(\d{2})[_-](\d{2})(\d{2})(\d{2})")
WHATSAPP_RE = re.compile(r"IMG-(20\d{2})(\d{2})(\d{2})-WA(\d+)", re.IGNORECASE)
ALREADY_DONE_RE = re.compile(
r"^\d{4}-\d\d-\d\d_(\d\d-\d\d-\d\d|WA\d+)(_\d+)?\.jpg$")
# --------------------------------------------------------------------------
# Minimal EXIF reader (JPEG APP1 segment -> TIFF structure -> date tags)
# --------------------------------------------------------------------------
def _parse_exif_date(text):
text = text.strip()[:19]
try:
return datetime.strptime(text, "%Y:%m:%d %H:%M:%S")
except ValueError: # empty, "0000:00:00 00:00:00", or garbage
return None
def _date_from_tiff(t):
"""Return (datetime, subseconds) from a TIFF/EXIF block, or (None, '')."""
if t[:2] == b"II":
e = "<"
elif t[:2] == b"MM":
e = ">"
else:
return None, ""
def read_ifd(offset):
tags = {}
(count,) = struct.unpack_from(e + "H", t, offset)
for i in range(count):
pos = offset + 2 + 12 * i
tag, typ, n = struct.unpack_from(e + "HHI", t, pos)
tags[tag] = (typ, n, t[pos + 8:pos + 12])
return tags
def ascii_value(entry):
typ, n, raw = entry
if typ != 2:
return ""
if n <= 4:
data = raw[:n]
else:
(off,) = struct.unpack(e + "I", raw)
data = t[off:off + n]
return data.split(b"\0")[0].decode("ascii", "ignore")
def long_value(entry):
return struct.unpack(e + "I", entry[2])[0]
(ifd0_off,) = struct.unpack_from(e + "I", t, 4)
ifd0 = read_ifd(ifd0_off)
exif = read_ifd(long_value(ifd0[0x8769])) if 0x8769 in ifd0 else {}
# DateTimeOriginal, then DateTimeDigitized (with their sub-second tags),
# then the generic DateTime as a last resort.
for date_tag, subsec_tag, ifd in ((0x9003, 0x9291, exif),
(0x9004, 0x9292, exif),
(0x0132, 0x9290, ifd0)):
if date_tag in ifd:
dt = _parse_exif_date(ascii_value(ifd[date_tag]))
if dt:
subsec = ascii_value(exif[subsec_tag]) if subsec_tag in exif else ""
return dt, subsec.strip()
return None, ""
def read_exif_datetime(path):
"""Return (datetime, subseconds) from a JPEG's EXIF data, or (None, '')."""
try:
with open(path, "rb") as f:
if f.read(2) != b"\xff\xd8":
return None, ""
while True:
byte = f.read(1)
if byte != b"\xff":
return None, ""
marker = f.read(1)
while marker == b"\xff": # padding bytes
marker = f.read(1)
if not marker:
return None, ""
m = marker[0]
if m == 0x01 or 0xD0 <= m <= 0xD8: # markers without length
continue
if m in (0xD9, 0xDA): # end of image / image data
return None, ""
(length,) = struct.unpack(">H", f.read(2))
segment = f.read(length - 2)
if m == 0xE1 and segment.startswith(b"Exif\0\0"):
return _date_from_tiff(segment[6:])
except (OSError, struct.error, IndexError, KeyError):
pass
return None, ""
# --------------------------------------------------------------------------
# Working out when each photo was taken
# --------------------------------------------------------------------------
def when_taken(path):
"""Return (sort_key_datetime, base_name_without_ext, source, subsec)."""
name = os.path.basename(path)
dt, subsec = read_exif_datetime(path)
if dt:
return dt, dt.strftime(OUT_FMT), "EXIF", subsec
m = ANDROID_RE.search(name)
if m:
try:
dt = datetime(*map(int, m.groups()))
return dt, dt.strftime(OUT_FMT), "filename", ""
except ValueError:
pass
m = WHATSAPP_RE.search(name)
if m:
y, mo, d, seq = m.groups()
try:
day = datetime(int(y), int(mo), int(d))
except ValueError:
day = None
if day:
mtime = datetime.fromtimestamp(os.path.getmtime(path))
if mtime.date() == day.date():
return mtime, mtime.strftime(OUT_FMT), "WhatsApp date + file time", ""
# Unknown time: sort after the rest of that day, keep WhatsApp order.
key = day.replace(hour=23, minute=59, second=59)
return key, f"{day:%Y-%m-%d}_WA{seq}", "WhatsApp date only", seq
return None, None, "no date found", ""
# --------------------------------------------------------------------------
# Planning, renaming, undoing
# --------------------------------------------------------------------------
def plan(folder):
entries, skipped = [], []
for name in sorted(os.listdir(folder)):
path = os.path.join(folder, name)
if not os.path.isfile(path) or name == LOG_NAME:
continue
if os.path.splitext(name)[1].lower() not in PHOTO_EXTS:
skipped.append((name, "not a .jpg"))
continue
if ALREADY_DONE_RE.match(name):
skipped.append((name, "already renamed"))
continue
key, base, source, subsec = when_taken(path)
if base is None:
skipped.append((name, source))
continue
entries.append((key, subsec, name, base, source))
# Names that will stay in the folder must not be overwritten.
renaming = {e[2] for e in entries}
taken = {n.lower() for n in os.listdir(folder) if n not in renaming}
# Photos taken in the same second get _2, _3 ... in shooting order.
entries.sort()
moves = []
for key, subsec, name, base, source in entries:
new, n = f"{base}.jpg", 1
while new.lower() in taken:
n += 1
new = f"{base}_{n}.jpg"
taken.add(new.lower())
moves.append((name, new, source))
return moves, skipped
def do_renames(folder, pairs):
"""Rename old->new via temporary names, so no file is ever overwritten."""
temps = []
for i, (old, new) in enumerate(pairs):
tmp = f".renaming-{i:05d}.tmp"
os.rename(os.path.join(folder, old), os.path.join(folder, tmp))
temps.append((tmp, new))
for tmp, new in temps:
os.rename(os.path.join(folder, tmp), os.path.join(folder, new))
def undo(folder):
log = os.path.join(folder, LOG_NAME)
if not os.path.exists(log):
sys.exit(f"No {LOG_NAME} in {folder}, nothing to undo.")
with open(log, newline="", encoding="utf-8") as f:
rows = list(csv.DictReader(f))
pairs = [(r["new_name"], r["original_name"]) for r in rows
if os.path.exists(os.path.join(folder, r["new_name"]))
and r["new_name"] != r["original_name"]]
do_renames(folder, pairs)
os.rename(log, log + ".undone")
print(f"Restored {len(pairs)} original names.")
# --------------------------------------------------------------------------
# Emailing a summary
# --------------------------------------------------------------------------
def load_mail_settings():
"""Read Gmail settings from the environment, else from MAIL_CONFIG."""
settings = {k: os.environ.get(k, "").strip() for k in MAIL_KEYS}
if not all(settings.values()) and os.path.exists(MAIL_CONFIG):
if os.stat(MAIL_CONFIG).st_mode & (stat.S_IRWXG | stat.S_IRWXO):
print(f"Warning: {MAIL_CONFIG} can be read by other users. "
f"Fix with: chmod 600 {MAIL_CONFIG}", file=sys.stderr)
with open(MAIL_CONFIG, encoding="utf-8") as f:
for line in f:
key, sep, value = line.strip().partition("=")
key = key.strip().removeprefix("export ").strip()
if sep and key in MAIL_KEYS and not settings[key]:
settings[key] = value.strip().strip("'\"")
missing = [k for k in MAIL_KEYS if not settings[k]]
if missing:
raise RuntimeError(f"Email settings missing: {', '.join(missing)}. "
f"Put them in {MAIL_CONFIG}")
return settings
def send_summary(subject, body):
s = load_mail_settings()
msg = EmailMessage()
msg["From"] = s["GMAIL_ADDRESS"]
msg["To"] = s["EMAIL_TO"]
msg["Subject"] = subject
msg.set_content(body)
# Google shows app passwords with spaces; the spaces are not part of it.
password = s["GMAIL_APP_PASSWORD"].replace(" ", "")
# Without an explicit context, SMTP_SSL does NOT check the server's
# certificate. create_default_context() verifies it against the system's
# trusted CAs and checks the hostname, so an impostor is refused before
# login() ever sends the password.
tls = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, timeout=60, context=tls) as smtp:
smtp.login(s["GMAIL_ADDRESS"], password)
smtp.send_message(msg)
print(f"Summary emailed to {s['EMAIL_TO']}.")
def main():
ap = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("folder", nargs="?", default=DEFAULT_FOLDER,
help="photo folder (default: the 'photos' folder next to this script)")
ap.add_argument("--apply", action="store_true", help="actually rename the files")
ap.add_argument("--undo", action="store_true", help="restore names from rename_log.csv")
ap.add_argument("--email", action="store_true",
help="after --apply, email a short summary (see top of file)")
args = ap.parse_args()
if args.email and not args.apply:
sys.exit("--email only works together with --apply.")
if not args.email:
result = run(args)
if isinstance(result, str):
sys.exit(result)
return
day = datetime.now().strftime("%d %b %Y")
try:
result = run(args)
except Exception as e: # still tell Papa that this week's run broke
result = f"Unexpected error: {e!r}"
if isinstance(result, int):
n = result
subject = f"Trip photos: {n} renamed ({day})"
body = (f"Hi Papa,\n\nThis week's photo tidy-up ran on {day}.\n"
f"{n} photo{'' if n == 1 else 's'} renamed "
f"in {os.path.abspath(args.folder)}.\n\n- Asha's photo script\n")
exit_msg = None
else:
subject = f"Trip photos: weekly rename FAILED ({day})"
body = (f"Hi Papa,\n\nThis week's photo tidy-up on {day} did not work:\n"
f"{result}\n\nNo photos were renamed.\n\n- Asha's photo script\n")
exit_msg = result
try:
send_summary(subject, body)
except (OSError, smtplib.SMTPException, RuntimeError, socket.error) as e:
print(f"Could not send email: {e}", file=sys.stderr)
sys.exit(1)
if exit_msg:
sys.exit(exit_msg)
def run(args):
"""Do the work. Returns the number of files renamed, or an error message."""
if not os.path.isdir(args.folder):
return f"Folder not found: {os.path.abspath(args.folder)}"
if args.undo:
undo(args.folder)
return 0
moves, skipped = plan(args.folder)
changes = [m for m in moves if m[0] != m[1]]
for old, new, source in moves:
print(f"{old:32} -> {new:32} [{source}]")
for name, why in skipped:
print(f"{name:32} SKIPPED ({why})")
print("\nWhere the dates came from:")
for source, count in Counter(m[2] for m in moves).most_common():
print(f" {count:4} {source}")
if skipped:
print(f" {len(skipped):4} skipped")
if not args.apply:
print(f"\nDry run: nothing changed. {len(changes)} files would be renamed. "
"Run again with --apply to rename.")
return 0
print(f"\n[{datetime.now():%Y-%m-%d %H:%M}]", end=" ")
if not changes:
print("Nothing new to rename.")
return 0
# Each run adds its renames to the log (written before renaming, for undo).
log = os.path.join(args.folder, LOG_NAME)
new_log = not os.path.exists(log)
with open(log, "a", newline="", encoding="utf-8") as f:
w = csv.writer(f)
if new_log:
w.writerow(["original_name", "new_name", "date_source"])
w.writerows(changes)
do_renames(args.folder, [(old, new) for old, new, _ in changes])
print(f"Renamed {len(changes)} files. Original names saved in {log} "
"(use --undo to restore).")
return len(changes)
if __name__ == "__main__":
main()
clean_expenses.py (prompt 2)
#!/usr/bin/env python3
"""Clean up the trip expenses CSV exported from Excel and print a summary.
Usage:
python3 clean_expenses.py [input.csv] [output.csv] [--keep-duplicates]
Defaults: trip_expenses.csv -> expenses_clean.csv
What it fixes:
* Excel's UTF-8 BOM and Windows line endings
* Blank rows
* Amounts like "₹3,450", "2,850" or " 320 " -> plain numbers
* Category spelling/case/spaces ("food ", "FOOD" -> "Food")
* Mixed date formats (1/11/2026, 02-11-2026) -> 2026-11-01
* Exact duplicate rows (removed, and listed so you can double-check)
* Rows with no usable amount (e.g. "-") are left out of totals and listed
The clean CSV is written as UTF-8 with a BOM so Excel shows ₹ and other
characters correctly, with dates in YYYY-MM-DD and amounts as plain numbers
so Excel treats them as real dates and numbers.
"""
import csv
import re
import sys
from collections import defaultdict
from datetime import datetime
from decimal import Decimal, InvalidOperation
from pathlib import Path
DATE_FORMATS = ("%d/%m/%Y", "%d-%m-%Y", "%d.%m.%Y", "%Y-%m-%d", "%d/%m/%y")
OUTPUT_COLUMNS = ["Date", "Item", "Category", "Amount", "Paid by"]
def clean_text(value):
"""Trim and collapse internal whitespace."""
return " ".join((value or "").split())
def parse_amount(raw):
"""'₹3,450' -> Decimal('3450'); returns None if there's no number."""
text = re.sub(r"[₹,\s]|Rs\.?|INR", "", raw or "", flags=re.IGNORECASE)
if not text or text in {"-", "–", "—"}:
return None
try:
return Decimal(text)
except InvalidOperation:
return None
def parse_date(raw):
text = clean_text(raw)
for fmt in DATE_FORMATS:
try:
return datetime.strptime(text, fmt).date()
except ValueError:
pass
return None
def fmt_money(amount):
amount = amount.quantize(Decimal("1")) if amount == amount.to_integral() else amount
return f"₹{amount:,}"
def load_rows(path):
# utf-8-sig strips the BOM Excel adds; newline="" lets csv handle CRLF.
with open(path, newline="", encoding="utf-8-sig") as f:
reader = csv.DictReader(f)
# Normalise header names (e.g. stray spaces, different case).
reader.fieldnames = [clean_text(h).title() for h in reader.fieldnames]
yield from ((reader.line_num, row) for row in reader)
def main(argv):
keep_dupes = "--keep-duplicates" in argv
args = [a for a in argv if not a.startswith("--")]
src = Path(args[0] if args else "trip_expenses.csv")
dst = Path(args[1] if len(args) > 1 else "expenses_clean.csv")
clean, problems, duplicates = [], [], []
seen = set()
for line_no, row in load_rows(src):
values = {k: clean_text(v) for k, v in row.items() if k}
if not any(values.values()):
continue # blank line
date = parse_date(values.get("Date"))
item = values.get("Item", "")
category = values.get("Category", "").title() or "Other"
person = values.get("Paid By", "").title() or "Unknown"
amount = parse_amount(values.get("Amount"))
if amount is None:
problems.append(f"line {line_no}: '{item}' has no amount "
f"({values.get('Amount')!r}) - left out")
continue
if date is None:
problems.append(f"line {line_no}: '{item}' has an unreadable date "
f"({values.get('Date')!r}) - kept with blank date")
key = (date, item.lower(), category, amount, person)
if key in seen:
duplicates.append(f"line {line_no}: {item}, {fmt_money(amount)}, {person}")
if not keep_dupes:
continue
seen.add(key)
clean.append({"Date": date, "Item": item, "Category": category,
"Amount": amount, "Paid by": person})
clean.sort(key=lambda r: (r["Date"] is None, r["Date"] or 0))
# utf-8-sig + CRLF = what Excel expects from a CSV.
with open(dst, "w", newline="", encoding="utf-8-sig") as f:
writer = csv.DictWriter(f, fieldnames=OUTPUT_COLUMNS, lineterminator="\r\n")
writer.writeheader()
for r in clean:
writer.writerow({**r,
"Date": r["Date"].isoformat() if r["Date"] else "",
"Amount": str(r["Amount"])})
# ---- Summary ----
total = sum((r["Amount"] for r in clean), Decimal(0))
by_cat, by_person = defaultdict(Decimal), defaultdict(Decimal)
for r in clean:
by_cat[r["Category"]] += r["Amount"]
by_person[r["Paid by"]] += r["Amount"]
def table(title, totals):
print(f"\n{title}")
width = max(len(k) for k in totals)
for name, amt in sorted(totals.items(), key=lambda kv: -kv[1]):
share = amt / total * 100 if total else 0
print(f" {name:<{width}} {fmt_money(amt):>10} ({share:4.1f}%)")
print(f"Cleaned {len(clean)} expenses -> {dst}")
print(f"\nTOTAL SPENT: {fmt_money(total)}")
table("By category:", by_cat)
table("By person (who paid):", by_person)
if duplicates:
action = "kept (--keep-duplicates)" if keep_dupes else "removed"
print(f"\nDuplicate rows {action} - please double-check these were really entered twice:")
for d in duplicates:
print(f" {d}")
if not keep_dupes:
print(" (run with --keep-duplicates if they were genuinely separate purchases)")
if problems:
print("\nNeeds attention:")
for p in problems:
print(f" {p}")
if __name__ == "__main__":
main(sys.argv[1:])
Common mistakes (real)
| Mistake | What you see (real) | Why | Fix |
|---|---|---|---|
os.rename to a name that already exists |
4 photos in, 3 photos out, no error | Two photos taken in the same second got the same name; rename replaced one silently | Check os.path.exists first and add _2, _3 |
| No dry run, no undo | (you find out after 312 files changed) | Scripts are fast at mistakes too | Dry run by default, --apply to act, an undo log, git commit first |
Reading an Excel CSV with encoding="utf-8" |
KeyError: 'Date' |
The first column is really '\ufeffDate' (a hidden BOM) |
encoding="utf-8-sig" |
float("₹3,450") |
ValueError: could not convert string to float: '₹3,450' |
₹ and , aren't digits |
Clean before counting (replace, strip), and use Decimal for money |
| Relative paths in a cron job | python3: can't open file '/home/asha/rename_photos.py', then Folder not found: photos |
cron starts in your home folder | Absolute paths, or paths built from the script's own folder (__file__) |
| No log for a cron job | nothing at all | cron has no screen | >> ~/cron.log 2>&1 |
| Password inside the script | (it ends up in Git and on screen) | Code gets shared, committed, screenshotted | A private settings file outside the project, chmod 600 |
smtplib.SMTP_SSL(...) with no context |
[fake server] LOGIN user=asha@example.com password=fakepassworddemo |
With no context, Python doesn't check the certificate | context=ssl.create_default_context() → CERTIFICATE_VERIFY_FAILED against a fake server |
Mini project
Automate one boring job on your own computer:
- Pick a folder that gets messy (Downloads, screenshots, WhatsApp images).
- Ask an AI coding tool for a script that sorts it (for example, into
2026-11month folders), with: a dry run by default,--apply, an undo log, never overwriting, and paths built from the script's own folder. - Commit or copy the folder first. Run the dry run and read every line. Test
--applyand--undoon a copy. - Schedule it with cron (
crontab -e) or Task Scheduler. Test with an every-minute line first, read the log, then switch to the real schedule and delete the test line. - Optional: email yourself a one-line summary with an app password stored outside the code, and test the certificate check against nothing but the real server.
Practice questions
- Why is renaming 312 photos by hand a job for a script?
Answer
It is the same rule applied over and over (find the time taken, write it as the name). A computer follows a rule perfectly and fast; a person takes about 52 minutes and makes typos.
- What is the difference between
photosand/home/asha/projects/trip-organiser/photos?
Answer
photos is a relative path: it means "photos inside the folder the program started in". The second is an absolute path: the same place wherever the program starts.
- In the obvious rename script, why did 4 photos become 3?
Answer
Two photos (Asha's and Mummy's group photo) were taken in the same second and got the same new name; os.rename replaced the first file without warning on Linux.
- What does this print? (Run it.)
import os
for name in ["a.txt", "b.txt"]:
open(name, "w").write(name)
os.rename("a.txt", "b.txt")
print(sorted(f for f in os.listdir(".") if f.endswith(".txt")), open("b.txt").read())
Answer
['b.txt'] a.txt on Linux and Mac: b.txt was replaced by the contents of a.txt, and a.txt is gone.
- What is a dry run, and why is it the default in the AI's script?
Answer
A rehearsal: the script prints what it would do and changes nothing. Making it the default means a mistake in the plan costs nothing; you add --apply only after reading the plan.
- Your CSV code says
KeyError: 'Date'but the file clearly has a Date column. What do you do?
Answer
Look instead of guessing: print the keys (print(list(row.keys()))). If you see '\ufeffDate', the file starts with a BOM; open it with encoding="utf-8-sig".
- Write a function that turns
"₹3,450"," 320 "and"2,850"into numbers.
Answer
from decimal import Decimal
def money(text):
return Decimal(text.replace("₹", "").replace(",", "").strip())
print(money("₹3,450"), money(" 320 "), money("2,850")) # 3450 320 2850
- Why should a cleaning script flag the duplicate kachori breakfast instead of silently deleting or keeping it?
Answer
The script can't know whether it was entered twice by mistake or really bought twice. Flag it, choose a safe default, and let a human decide (the AI's script removes it, lists it, and offers --keep-duplicates).
- What does
30 7 * * 1-5mean in a crontab?
Answer
At 7:30 am, Monday to Friday (minute 30, hour 7, any day of the month, any month, days of the week 1 to 5).
- A cron job prints nothing and you can't tell if it ran. What's missing?
Answer
A log: cron has no screen. Add >> /home/you/job.log 2>&1 so normal output and errors are saved to a file.
- Where should the Gmail app password live, and why there?
Answer
In a private settings file outside the project (here ~/.config/trip-organiser/mail.env, chmod 600), or an environment variable. Outside the project means Git never commits it; 600 means other users on the computer can't read it.
- The email code uses
smtplib.SMTP_SSL, so it's encrypted. Why was the password still stolen by the fake server?
Answer
Encrypted is not the same as verified. With no context, Python 3.13's SMTP_SSL uses an unverified context: it never checks that the server's certificate is real and for smtp.gmail.com, so it happily logged in to an impostor. ssl.create_default_context() adds the check.
Quiz
Your script works when you run it in the terminal, but cron logs Folder not found: photos. Why?
- A) Cron can't run Python scripts
- B) Cron starts the job in your home folder, so a relative path points somewhere else
- C) The crontab line has too many stars
- D) The photos are locked while cron runs
Answer
B. Cron runs the job from your home folder with a small environment, so "photos" means /home/asha/photos. Use an absolute path, or build the path from the script's own location.
Interview questions
- What makes a script safe to run on someone's real files? A dry run by default, never overwriting (check before rename), an undo log, a backup or Git commit first, and a clear summary of what changed and what was skipped.
- Why do scripts behave differently under cron? cron starts jobs in the user's home folder with a minimal environment (a short
PATH,SHELL=/usr/bin/sh), and no terminal. Use absolute paths, set any needed variables in the crontab, and log output with>> file 2>&1. - How do you keep credentials out of an automation? Read them from environment variables or a private settings file outside the repository (permissions
600), use revocable app-specific passwords or tokens, never print them, and never commit them. - What does certificate verification protect against, and how do you turn it on in Python's smtplib? It stops a man-in-the-middle (an impostor server) from receiving your credentials: the client checks the certificate is signed by a trusted CA and matches the host name. Pass
context=ssl.create_default_context()toSMTP_SSL(or tostarttls()). - Why use
Decimalrather thanfloatfor money?floatstores numbers in binary and can't represent many decimal values exactly (0.1 + 0.2 is 0.30000000000000004), so totals can drift;Decimalkeeps exact decimal amounts. - How would you schedule the same job on Windows? Task Scheduler, from the UI or with
schtasks /create /tn <name> /tr "<full path to program and script>" /sc weekly /d SUN /st 21:00.
Glossary
| Term | Meaning (plain words) | Example |
|---|---|---|
| Script | A text file of steps the computer follows, like a recipe card | rename_photos.py |
| Python | An easy-to-read programming language; python3 runs Python files |
python3 rename_photos.py |
| Standard library | The tools that come with Python, nothing to install | os, csv, smtplib, ssl |
| Automation | Letting the computer do a repeated job by itself, often on a timetable | Renaming photos every Sunday at 9 pm |
| File | A named box of data on disk | IMG_4801.JPG |
| Folder (directory) | A named group of files | photos |
| Path | The address of a file or folder | /home/asha/projects/trip-organiser/photos |
| Absolute path | A path from the top (/ or C:\); means the same place from anywhere |
/home/asha/cron.log |
| Relative path | A path read from the current folder | photos |
| Working directory | The folder a program starts in; relative paths are read from it | cron starts jobs in /home/asha |
open(..., "w") |
Open a file for writing (creates or empties it) | open("shopping.txt", "w") |
os.listdir |
List the names in a folder | os.listdir(".") |
os.rename |
Give a file a new name; on Linux it replaces an existing file with that name | os.rename("IMG_4825.JPG", "2026-10-30_17-42-09.jpg") |
| EXIF | Information a camera saves inside a photo, such as when it was taken | DateTimeOriginal 2026:10:30 08:11:18 |
| Dry run | A rehearsal: show the plan, change nothing | python3 rename_photos.py (no --apply) |
| Undo log | A record of old and new names so changes can be reversed | photos/rename_log.csv |
| Flag / option | An extra word on the command line that changes what a program does | --apply, --undo, --email |
| CSV | Comma-separated values: a table saved as plain text | 30/10/2026,Lassi,food ,120,Asha |
| Data cleaning | Making every value follow one format; flagging what can't be fixed | "₹3,450" → 3450, food → Food |
| Duplicate | The same record entered twice | Kachori breakfast, ₹160, twice |
| Dictionary | Name → value pairs | row["Amount"] |
KeyError |
Python's error for "no entry with exactly that name" | KeyError: 'Date' |
ValueError |
Python's error for "this value has the wrong form" | could not convert string to float: '₹3,450' |
| Encoding | The rule for turning characters into bytes and back | UTF-8 |
| BOM (byte order mark) | An invisible character at the start of some UTF-8 files | '\ufeffDate'; read with utf-8-sig |
float |
A number with decimals, stored in binary (not exact for money) | float("120") → 120.0 |
Decimal |
An exact decimal number, good for money | Decimal("3450") |
| cron | A Linux/Mac program that runs jobs on a timetable | Runs the rename every Sunday |
| crontab | Your list of cron jobs (crontab -e to edit, -l to list, -r to remove all) |
0 21 * * 0 python3 ... |
| Cron time fields | minute, hour, day of month, month, day of week (0 or 7 = Sunday); * = every |
0 21 * * 0 = Sundays 9 pm |
>> and 2>&1 |
Append output to a file; send errors to the same place | >> /home/asha/cron.log 2>&1 |
Task Scheduler / schtasks |
Windows' built-in scheduler and its command-line tool | schtasks /create /tn MyApp /tr c:\apps\myapp.exe /sc weekly /d WED |
__file__ |
The path of the running Python script | os.path.dirname(os.path.abspath(__file__)) |
~ (tilde) |
Your home folder | ~/.config/trip-organiser/mail.env |
chmod / 600 |
Change who can read/write a file; 600 = only the owner |
-rw------- |
| SMTP | The protocol (language) for sending email | smtp.gmail.com, port 465 |
| App password | A 16-digit, revocable passcode for one app; needs 2-Step Verification | Stored in mail.env, never in code |
| 2-Step Verification | Sign-in needs your password plus a second proof | A code on your phone |
| TLS / SSL | Encryption for network connections (SSL is the old name) | smtplib.SMTP_SSL |
| Certificate | A server's ID card, signed by a trusted authority | "I am smtp.gmail.com", signed by a CA |
| Certificate authority (CA) | An organisation your computer trusts to sign certificates | Listed in the system's trusted CAs |
| Self-signed certificate | A certificate signed by its own server; not trusted | Our fake server's |
| SSL context | The bundle of TLS settings (trusted CAs, name checks) | ssl.create_default_context() |
CERTIFICATE_VERIFY_FAILED |
The error when a certificate fails the check | self-signed certificate (_ssl.c:1035) |
| Fail closed | When a safety check fails, stop instead of carrying on | No email rather than a leaked password |
| Man-in-the-middle | An impostor between you and the real server | A fake smtp.gmail.com on bad Wi-Fi |
Summary
- A job that applies the same rule many times is a job for a script. Python's standard library can read, write and rename files, read CSVs and send email.
- Scripts are fast at mistakes too: the obvious rename lost a photo silently. Never overwrite, give every script a dry run, an undo log, and commit the originals first.
- Real data is messy: a hidden BOM broke the column name,
₹and commas broke the numbers. Clean before counting, flag duplicates and missing values, check totals another way. - cron runs jobs on a timetable (
0 21 * * 0), but starts them in your home folder: use absolute paths or paths next to the script, and log the output. On Windows, Task Scheduler (schtasks). - Notifications safely: secrets outside the code (
chmod 600), app passwords, and verify the server's certificate. The AI'sSMTP_SSLcall without a context handed the password to a fake server;ssl.create_default_context()made it refuse. - Next episode: debugging when the AI is stuck.