File & Folder Automation
Build Python scripts that organize files into folders, rename files in bulk, create automatic backups, and monitor directories for changes — all with AI assistance.
🔄 Recall Bridge: In the previous lesson, you learned why Python is ideal for automation and how AI generates scripts from natural language descriptions. Now let’s build your first real automation — organizing and managing files.
File management is the most common automation starting point because the results are immediately visible, the risk is low (you can always move files back), and everyone has a messy Downloads folder.
Script 1: File Organizer by Type
AI prompt:
Write a Python script using pathlib that organizes files in a specified folder into subfolders by file type. Rules: (1) Images (.jpg, .png, .gif, .webp, .svg) → “Images/”, (2) Documents (.pdf, .docx, .xlsx, .pptx, .txt) → “Documents/”, (3) Videos (.mp4, .mov, .avi, .mkv) → “Videos/”, (4) Archives (.zip, .tar, .gz, .rar) → “Archives/”, (5) Everything else → “Other/”. Include: dry-run mode, CSV logging, skip hidden files, handle duplicate filenames by appending _1, _2, etc. Use argparse for command-line arguments.
Key pathlib operations for file automation:
| Operation | pathlib Code | What It Does |
|---|---|---|
| List files | Path(folder).iterdir() | Iterate over all items |
| Filter by type | Path(folder).glob("*.pdf") | Find files matching a pattern |
| Get extension | path.suffix | Returns .pdf, .jpg, etc. |
| Get filename | path.stem | Filename without extension |
| Move file | path.rename(new_path) | Move or rename |
| Create folder | path.mkdir(parents=True, exist_ok=True) | Create nested folders safely |
| Check exists | path.exists() | Check if file/folder exists |
Script 2: Bulk File Renamer
AI prompt:
Write a Python script that renames files in bulk. Support these renaming patterns: (1) Add date prefix from file metadata: IMG_1234.jpg → 2026-01-15_IMG_1234.jpg, (2) Replace text in filenames: “Screenshot_” → “screen-”, (3) Sequential numbering: photo.jpg → vacation_001.jpg. Include: preview mode (show old → new without renaming), undo capability (save a mapping file to reverse the operation), skip files that already match the pattern. Accept folder path and pattern as command-line arguments.
The preview-then-execute pattern:
# This pattern applies to ALL destructive automation
changes = []
for file in folder.glob("*"):
new_name = generate_new_name(file)
changes.append((file, new_name))
# Preview
for old, new in changes:
print(f" {old.name} → {new.name}")
# Confirm
if input("Proceed? (y/n): ").lower() == "y":
for old, new in changes:
old.rename(new)
Script 3: Automatic Backup
AI prompt:
Write a Python script that backs up a specified folder to a destination with the format: backup_YYYY-MM-DD_HHMMSS/. Include: (1) Only copy files modified since the last backup (incremental backup), (2) Skip files matching patterns in a .backupignore file (like .gitignore), (3) Print a summary: files copied, total size, time elapsed, (4) Option to compress the backup as a .zip file. Use shutil for file operations and argparse for command-line arguments.
Essential File Automation Patterns
| Pattern | When to Use | Example |
|---|---|---|
| Dry-run mode | Any script that modifies files | Preview what will happen before executing |
| Logging | Scripts processing many files | CSV log of every action for troubleshooting |
| Error continuation | Batch operations | try/except per file, don’t stop on one failure |
| Duplicate handling | Moving/copying files | Append _1, _2 or timestamp to avoid overwrites |
| Undo capability | Renaming operations | Save old→new mapping for reversal |
✅ Quick Check: Your script needs to handle a file named
my résumé (final).pdf— notice the accent, spaces, and parentheses. Will pathlib handle this correctly? (Answer: Yes, pathlib handles Unicode characters, spaces, and special characters correctly on all operating systems. The common pitfall is using string concatenation instead of pathlib’s/operator:Path(folder) / filenamehandles special characters safely, while f-string concatenation might not escape properly on all platforms.)
Practice Exercises
- Easy: Write a script that finds all files larger than 100MB in a folder and lists them sorted by size
- Medium: Write a script that monitors a folder and automatically organizes new files as they appear (use
time.sleep()to poll every 5 seconds) - Challenge: Write a script that finds duplicate files by comparing file hashes (not just names) and moves duplicates to a “Duplicates” folder
AI prompt for any exercise:
Write a Python script that [DESCRIBE THE TASK]. Use pathlib for file operations. Include: error handling for each file, a summary of what was done, and a dry-run mode. Make the script runnable from the command line with argparse.
Key Takeaways
- Always include a dry-run mode in file automation scripts — previewing what will happen before executing protects you from accidental data loss and lets you verify the script’s behavior on edge cases like duplicate filenames, special characters, and files without extensions
- Use pathlib (not os.path) for modern, readable file operations — it treats paths as objects, handles cross-platform compatibility automatically, and produces code that reads like English (
path.suffix,path.stem,path.parent) - Test file scripts on a small copy before running on real data — 20 test files won’t reveal the edge cases hiding in 3,000 real files, so progress through dry-run → copy test → real execution to catch problems safely
Up Next
In the next lesson, you’ll learn to process data files — reading, transforming, and writing CSV, Excel, and JSON with AI-generated pandas scripts.
Knowledge Check
Complete the quiz above first
Lesson completed!