Lesson 2 12 min

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:

Operationpathlib CodeWhat It Does
List filesPath(folder).iterdir()Iterate over all items
Filter by typePath(folder).glob("*.pdf")Find files matching a pattern
Get extensionpath.suffixReturns .pdf, .jpg, etc.
Get filenamepath.stemFilename without extension
Move filepath.rename(new_path)Move or rename
Create folderpath.mkdir(parents=True, exist_ok=True)Create nested folders safely
Check existspath.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

PatternWhen to UseExample
Dry-run modeAny script that modifies filesPreview what will happen before executing
LoggingScripts processing many filesCSV log of every action for troubleshooting
Error continuationBatch operationstry/except per file, don’t stop on one failure
Duplicate handlingMoving/copying filesAppend _1, _2 or timestamp to avoid overwrites
Undo capabilityRenaming operationsSave 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) / filename handles special characters safely, while f-string concatenation might not escape properly on all platforms.)

Practice Exercises

  1. Easy: Write a script that finds all files larger than 100MB in a folder and lists them sorted by size
  2. Medium: Write a script that monitors a folder and automatically organizes new files as they appear (use time.sleep() to poll every 5 seconds)
  3. 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

1. You're writing a script to organize 5,000 files in your Downloads folder. You use os.rename() to move files into subfolders by type. The script crashes halfway through after moving 2,300 files. Now your Downloads folder is a mess — some files moved, some didn't. How do you prevent this?

2. You want to rename 200 photos from 'IMG_1234.jpg' to '2026-01-15_vacation_001.jpg' format. You ask AI to write the script. It uses os.path for path manipulation. Your colleague says you should use pathlib instead. Who's right?

3. Your file organizer script works perfectly on your test folder of 20 files. You're about to run it on your actual Downloads folder with 3,000 files. What should you do first?

Answer all questions to check

Complete the quiz above first

Related Skills