▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
Processing files with AI
Summarise or classify every file in a folder.
🎬 Video version coming soon — read it now
The real power of AI in Python is scale: running the same task over a whole folder of files. Let's classify a folder of customer feedback.
The setup
Imagine a folder feedback/ with one .txt file per customer message for a restaurant in MVP Colony.
Loop over the folder
model_dump() turns the Pydantic object into a dictionary, and ** merges it into the row.
Save and summarise
A manager now gets a spreadsheet plus a list of customers to call back: a real business tool from about 40 lines of Python.
Before you run it on 1,000 files
- Test on 5 files first and read the results yourself. Fix the prompt, then scale.
- Estimate the cost: check
response.usageon your test files and multiply. - Save as you go: append each result to the CSV inside the loop, so a crash at file 600 doesn't lose 599 results.
- Skip finished files on a re-run, by checking which names are already in the CSV.
- Mind privacy: customer messages can contain names and phone numbers. Check your company's rules before sending them to any external API.
Big jobs: batches
For very large jobs that don't need answers immediately, providers offer batch APIs: send thousands of requests at once and collect the results later, usually at a discount. Look it up when you need it.
In one line
Wrap the AI call in a function, loop over files with pathlib, save structured results to CSV, and always test on a few files before scaling.
Share with a friend