Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
436: Building an Automated Screenshot Organizer
A few years ago, I was tasked with auditing the UI of a legacy enterprise dashboard. Every time I spotted a misalignment or a broken button, I’d snap a screenshot. By the end of the first sprint, my desktop was a disaster zone—literally hundreds of files named Screenshot 2021-05-12 at 11.24.10 AM.png. When my project manager asked for the "Tuesday bugs" specifically, I spent nearly half an hour just scrolling and squinting at timestamps. It was a mindless, frustrating waste of time, so I spent my lunch break writing a Python script to handle the sorting for me. I never manually moved a screenshot again.
The goal here isn't just to move files, but to create a system that understands the metadata hidden inside the filename. Most OS-generated screenshots follow a predictable naming convention. If we can parse that string, we can programmatically generate a folder hierarchy that makes sense—like Screenshots/2023/October/—and slide the files in there automatically.
Taming the Desktop Chaos with Pathlib
While you could use the older os module, I strongly recommend pathlib for this. It treats paths as objects rather than strings, which makes your code much cleaner and less prone to those annoying trailing-slash errors. To start, we need to target our source folder and filter for files that actually look like screenshots.
from pathlib import Path
import shutil
# Target the directory where your screenshots live
source_dir = Path.home() / "Desktop"
# Use a glob pattern to find only the PNGs that start with 'Screenshot'
screenshots = list(source_dir.glob("Screenshot *.png"))
print(f"Found {len(screenshots)} screenshots to organize.")
Notice the use of .glob(). It's far more efficient than listing every file in the directory and then running an if "Screenshot" in filename check. It lets the filesystem do the heavy lifting for us.
Extracting Date Patterns and Routing Files
Once we have our list of files, the magic happens in the string parsing. Since the date is baked into the filename (usually in YYYY-MM-DD format), we can slice that string to create our folder names. I prefer using slicing here because it's faster than a full regex when the format is guaranteed to be consistent.
for file_path in screenshots:
# Example filename: "Screenshot 2023-10-25 at 12.00.00.png"
# The date starts at index 12 and ends at 22
filename = file_path.name
date_part = filename[12:22] # "2023-10-25"
year, month, day = date_part.split('-')
# Define the destination path: Screenshots/Year/Month
dest_dir = Path.home() / "Documents" / "Screenshots" / year / month
# This is the critical part: parents=True creates the whole tree
# exist_ok=True prevents the script from crashing if the folder already exists
dest_dir.mkdir(parents=True, exist_ok=True)
# Move the file to the new home
shutil.move(str(file_path), str(dest_dir / filename))
I used shutil.move here instead of Path.rename because shutil is generally more robust when moving files across different partitions or drives. A quick tip: always test this on a backup folder first. There is nothing quite as heart-stopping as a bug in a file-moving script that accidentally sends your important documents into a black hole of nested folders.
📋 Practical Task
The Quarterly Bug-Shot Archivist
Build a Python script that organizes a folder of mock screenshot files. To test this, first create a folder named test_vault and populate it with 10-15 empty files named using the pattern Screenshot YYYY-MM-DD at HH.MM.SS.png (ensure you use various months and years).
Your script must:
- Scan the
test_vaultdirectory. - Parse the filename to extract the Year, Month, and Day.
- Move the files into a directory structure that looks like:
Archived_Screenshots/YYYY/MM/DD/filename.png. - Print a confirmation message for every file moved, indicating its new destination.
There are no comments for now.