Writing Scripts for Automation with AI — Guided Lab
Welcome to the Lab 🛠️
Manual repetitive tasks can take hours. AI can act as a coding co-pilot, helping you write scripts that automate these tasks. Today, you’ll generate Python scripts for real-world automation, test them, and refine them interactively.
Step 0: Define the Task
Scenario: You receive daily CSV reports and want to automatically rename, filter, and summarize them.
- Input: Folder of CSV files
- Output: Summary CSV containing key stats
- Optional: Log errors or skipped files
Mini-challenge: Write down, in plain English, what you want the script to do.
Step 1: Craft a Prompt for the AI
Good prompts include:
- Task description
- Input/output format
- Optional constraints (performance, logging, style)
Example Prompt:
Write a Python script that reads all CSV files in a folder, filters out rows where "Status" is "Inactive", and writes a summary CSV with the counts of active items. Include error handling and logging.
Step 2: AI Output Example
import pandas as pd
import os
summary = []
for file in os.listdir('data'):
if file.endswith('.csv'):
try:
df = pd.read_csv(os.path.join('data', file))
active_count = df[df['Status'] == 'Active'].shape[0]
summary.append({'file': file, 'active_count': active_count})
except Exception as e:
print(f"Error processing {file}: {e}")
summary_df = pd.DataFrame(summary)
summary_df.to_csv('summary.csv', index=False)
print("Summary created successfully.")
Step 3: Test the Script
Run the script on sample data:
python automate_reports.py
- Check the summary output
- Note any errors or missing functionality
- Update the prompt to include additional rules (e.g., sorting, multiple columns, or logging to a file)
Step 4: Mini Lab Challenges
- Add a feature to sort the summary CSV by
active_countdescending. - Modify the script to handle multiple folders automatically.
- Include logging to a file instead of printing errors.
- Experiment with prompts for Excel automation, sending emails, or PDF generation.
Step 5: Pro Tips
- Break large automation tasks into smaller steps for the AI.
- Provide examples if the task is complex.
- Test the script thoroughly — AI may miss edge cases.
- Combine AI-generated scripts with your own code for robust automation.
- Iteratively refine prompts to improve readability, efficiency, and error handling.
Lab Summary
- AI can generate full automation scripts from plain-English instructions.
- Clear prompts = task description + inputs/outputs + constraints.
- Iteration is key: refine prompts → test → improve.
- Using AI for scripting saves time and allows you to focus on higher-value tasks.


