Python Programming for Applied Projects
Automation
Putting Python to Work
So far, you've learned the building blocks of Python. Now it's time to use them to make your computer do your bidding. The real power of programming isn't just solving problems, it's solving them automatically, over and over again, without you lifting a finger.
In this fully revised third edition of Automate the Boring Stuff with Python, you’ll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required.
This is the core idea of automation. We'll write scripts to handle tedious, repetitive tasks so we can focus on more interesting things. Let's start with one of the most common chores: managing files.
Automating Your File System
You already know how to read and write the contents of files. But what about managing the files themselves? Python has powerful built-in modules for interacting with your computer's operating system, allowing you to create, rename, move, and delete files and folders just like you would with your mouse.
Two of the most useful modules for this are os and shutil. The os module lets you interact with the operating system, like getting a list of files in a directory. The shutil module (short for "shell utilities") gives you high-level operations like copying and moving files.
Think of
osas a way to look around and get information, andshutilas the tool you use to actually move things.
Let's imagine your 'Downloads' folder is a mess. It's full of images, documents, and other random files. We can write a script to automatically organize them into subfolders.
import os
import shutil
# The path to your messy downloads folder
# Replace with your actual path
DOWNLOADS_PATH = "/Users/your_username/Downloads"
# Define where different file types should go
DESTINATIONS = {
"images": [".jpg", ".jpeg", ".png", ".gif"],
"documents": [".pdf", ".docx", ".txt"],
"music": [".mp3", ".wav"]
}
# Get a list of all files in the downloads folder
files = os.listdir(DOWNLOADS_PATH)
for filename in files:
file_path = os.path.join(DOWNLOADS_PATH, filename)
# Skip directories, only process files
if os.path.isfile(file_path):
# Check for each destination type
for folder_name, extensions in DESTINATIONS.items():
for ext in extensions:
if filename.endswith(ext):
# Create the destination folder if it doesn't exist
dest_folder_path = os.path.join(DOWNLOADS_PATH, folder_name)
os.makedirs(dest_folder_path, exist_ok=True)
# Move the file
shutil.move(file_path, dest_folder_path)
print(f"Moved {filename} to {folder_name}/")
This script first lists everything in the target directory. Then, for each item, it checks if it's a file and what its extension is. If the extension matches one of our categories, it creates a corresponding folder (if needed) and moves the file into it. Run a script like this once a day, and you'll never have a messy downloads folder again.
Working with Web Services
Many of our daily tasks involve the internet. Automation isn't limited to your local files; you can also write scripts that interact with services across the web. The key to this is the Application Programming Interface, or API.
An API is essentially a set of rules and tools that allows different software applications to communicate with each other. Think of it like a restaurant menu. You don't need to know how the kitchen works; you just pick an item from the menu (make a request), and the kitchen sends back your food (the data).
To interact with web APIs in Python, the requests library is the standard choice. It makes sending HTTP requests incredibly simple. You'll likely need to install it first. Once installed, you can use it to fetch data from a public API. Let's try one that gives us random facts about cats.
import requests
import json
# The URL for the API endpoint
API_URL = "https://cat-fact.herokuapp.com/facts/random"
try:
# Make a GET request to the API
response = requests.get(API_URL)
# Raise an exception for bad status codes (like 404 or 500)
response.raise_for_status()
# The response content is in JSON format, so we parse it
data = response.json()
# Print the fact
print(data['text'])
except requests.exceptions.RequestException as e:
print(f"Could not connect to the API: {e}")
This script sends a GET request to the specified URL. If the request is successful, the server sends back data, which we parse from a format called JSON into a familiar Python dictionary. We can then easily access the data we want, in this case, the cat fact stored under the 'text' key.
This is the foundation for automating tasks like checking for new emails, getting stock prices, or posting updates to social media, all without opening a web browser.
Scheduling Your Scripts
An automation script is most powerful when you don't have to remember to run it. Scheduling allows you to run your Python scripts automatically at specific times or intervals. You could run a file cleanup script every night at midnight, or a stock checker every hour during the trading day.
While operating systems have built-in tools for this (like Cron on macOS/Linux and Task Scheduler on Windows), you can also handle scheduling directly within Python using libraries. A simple and readable one is called schedule.
import schedule
import time
def organize_downloads():
print("Time to organize the downloads folder! Running script...")
# In a real script, you would put the file-moving code here.
# Schedule the job to run every day at 10:30 PM
schedule.every().day.at("22:30").do(organize_downloads)
# Schedule a different job to run every hour
schedule.every().hour.do(lambda: print("Hourly check-in."))
print("Scheduler started. Waiting to run tasks...")
while True:
schedule.run_pending()
time.sleep(1) # Wait one second before checking again
The schedule library provides a wonderfully fluent way to define your tasks. The while True: loop at the end is crucial; it keeps the script running continuously, checking every second to see if a scheduled task is due. Without it, the program would schedule the tasks and then immediately exit.
With these tools for file management, web interaction, and scheduling, you have a solid foundation for automating a vast range of digital chores.
Which Python module is best suited for high-level file operations, such as copying an entire folder from one location to another?
What is the primary role of an Application Programming Interface (API) in the context of web automation?
This concludes our journey through the fundamentals of Python. You've gone from setting up your environment to writing scripts that can automate your life. The possibilities from here are endless.