Skip to content
IT Vizag
← Python Meets AI

▶ 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

Lesson 7 of 10 · 2 min read

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.

🔒 Code example: sign up free (above) to unlock it.

Loop over the folder

🔒 Code example: sign up free (above) to unlock it.

model_dump() turns the Pydantic object into a dictionary, and ** merges it into the row.

Save and summarise

🔒 Code example: sign up free (above) to unlock it.

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.usage on 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.

Tip: Make the same loop work for PDFs, emails or support tickets. Only the reading step and the Pydantic shape change.

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.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend