How to Write a Python Automation Script With ChatGPT
Go from a plain-English description of a repetitive task to a working Python script you can run and schedule.
Renaming a folder of files, merging CSVs, downloading reports: these are five-minute tasks done by hand and zero-minute tasks done by a script. ChatGPT can write that script even if you barely know Python, as long as you describe the task precisely and test what it gives you.
- Python installed on your machine (run python3 --version to check)
- A clear description of the input, the action, and the output
- A safe place to test, such as a copy of your files
Step 1: Describe the task like a spec
The more concrete your description, the closer the first draft is to working. State the input location, the exact transformation, the output, and any edge cases. Ambiguity here becomes bugs later.
Write a Python 3 script that:
- Looks in a folder ./invoices for all .csv files
- Merges them into one file all_invoices.csv
- Adds a column 'source_file' with each row's original filename
- Skips files that are empty and prints which were skipped
Use only the standard library. Add comments and a usage note.Step 2: Review the script before running it
Read what it produced. Check that it points at the right folder and does not overwrite or delete anything you care about. Never run a script blind, especially one that touches files.
import csv, glob, os
# Usage: python3 merge_invoices.py
INPUT_DIR = "./invoices"
OUTPUT = "all_invoices.csv"
rows, header, skipped = [], None, []
for path in glob.glob(os.path.join(INPUT_DIR, "*.csv")):
with open(path, newline="") as f:
reader = list(csv.reader(f))
if not reader:
skipped.append(path)
continue
if header is None:
header = reader[0] + ["source_file"]
name = os.path.basename(path)
for r in reader[1:]:
rows.append(r + [name])
with open(OUTPUT, "w", newline="") as f:
w = csv.writer(f)
if header:
w.writerow(header)
w.writerows(rows)
print(f"Merged {len(rows)} rows into {OUTPUT}")
if skipped:
print("Skipped empty files:", skipped)Step 3: Run it on a copy first
Test against duplicated data so a mistake costs nothing. Run the script and read the output. If it errors, paste the full error back into the chat and ask for a fix.
Step 4: Schedule it to run on its own
Once it works, ask ChatGPT how to schedule it. On Mac or Linux that is usually cron; on Windows, Task Scheduler. Tell it your OS and how often, and it gives you the exact entry to add.
Result
A repetitive merge job that took you ten minutes by hand is now a tested script that runs every weekday morning by itself. You wrote it without being a Python expert because you described the task clearly and verified each step.
Watch related tutorials
16:12
25:00
14:00
15:00
1:42:18
28:14