
Quick preview: This guide teaches you how to identify automation opportunities, set up a Python environment, and build five practical automation scripts (files, web requests, scraping, GUI automation, scheduling). Each example includes code you can copy, adapt and run. Recommended reading and further resources are cited so you can verify and expand your learning.
Why automate with Python?
Python is one of the easiest and most widely used languages for automation: its readable syntax, huge ecosystem of libraries (requests, BeautifulSoup, pandas, pyautogui, selenium, schedule, etc.), and strong beginner resources make it ideal for turning repetitive tasks into scripts you can run in minutes. Python is also among the top languages used by developers worldwide, making community help and libraries easy to find. survey.stackoverflow.co+1
Table of contents
- Identify tasks worth automating
- Setup: install Python & create a virtual environment
- Example 1 — File & folder automation (rename, move, batch edit)
- Example 2 — Interacting with APIs (download & process JSON)
- Example 3 — Web scraping basics (BeautifulSoup + requests)
- Example 4 — GUI automation (PyAutoGUI)
- Example 5 — Scheduling scripts (schedule + cron)
- Best practices, safety and next steps
- Resources & recommended reading
1. Identify tasks worth automating
Automate tasks that are:
- Repetitive and rule-based (e.g., renaming many files).
- Time-consuming but straightforward (e.g., daily report generation).
- Error-prone when done manually (e.g., copying data between systems).
If a task takes >5 minutes and you’ll do it more than a few times, it’s a good automation candidate. Zapier’s roundup of common Python automation ideas (file handling, API tasks, web scraping, reformatting data) is a great checklist when you brainstorm automation projects.
2. Setup: install Python & create a virtual environment
- Install Python 3.10+ from python.org (or use your package manager).
- Open a terminal / command prompt and verify installation:
python --version
- Create a project folder and virtual environment:
mkdir py-automation
cd py-automation
python -m venv .venv
# Activate:
# macOS/Linux: source .venv/bin/activate
# Windows PowerShell: .venv\Scripts\Activate.ps1
pip install --upgrade pip
- Install common libraries used in examples:
pip install requests beautifulsoup4 pandas openpyxl pyautogui schedule
Tip: use a code editor like VS Code and run the scripts from the integrated terminal. See Real Python for a friendly terminal primer if you’re new to command-line workflows.
3. Example 1 — File & folder automation
Goal: Batch rename all .txt files to add a prefix (e.g., processed_).
# batch_rename.py
from pathlib import Path
source = Path('to_rename')
for p in source.glob('*.txt'):
new_name = p.with_name(f'processed_{p.name}')
p.rename(new_name)
print(f'Renamed: {p.name} -> {new_name.name}')
Run:
python batch_rename.py
Variants: move files by extension into folders, add timestamps, or update metadata. pathlib is cross-platform and preferred over os.path for clarity.
4. Example 2 — Interacting with APIs (download JSON, save CSV)
Goal: Fetch JSON from a public API and write selected fields to CSV.
# fetch_api.py
import requests
import csv
url = 'https://api.publicapis.org/entries' # example public API
r = requests.get(url, timeout=10)
r.raise_for_status()
data = r.json()['entries']
with open('apis_summary.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['API', 'Description', 'Auth', 'HTTPS', 'Category'])
for e in data:
writer.writerow([e.get('API'), e.get('Description'), e.get('Auth'), e.get('HTTPS'), e.get('Category')])
print('Saved apis_summary.csv')
Notes: Always check API rate limits and whether an API requires authentication (API keys). requests is the de-facto standard for HTTP in Python. Qodo
5. Example 3 — Web scraping basics (requests + BeautifulSoup)
Goal: Scrape headlines from a simple news page and save them.
# scrape_headlines.py
import requests
from bs4 import BeautifulSoup
url = 'https://realpython.com/' # example site
r = requests.get(url, timeout=10)
r.raise_for_status()
soup = BeautifulSoup(r.text, 'html.parser')
headlines = [h.get_text(strip=True) for h in soup.select('h2')][:10]
with open('headlines.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(headlines))
print('Saved headlines.txt')
Ethics & legality: Respect robots.txt, site terms, and rate limits. For complex sites with JavaScript-loaded content, consider Selenium or APIs when available. There are many beginner guides that cover scraping responsibly and show how to parse HTML safely. Medium+1
6. Example 4 — GUI automation with PyAutoGUI
Goal: Automate a simple GUI action — open a text editor, type text, save the file.
Warning: GUI automation simulates mouse/keyboard — ensure your screen layout is predictable and close sensitive windows before testing.
# gui_automate.py
import pyautogui
import time
time.sleep(2) # switch to the screen where you want to run the actions
pyautogui.hotkey('win', 'r') # Windows: open Run dialog
time.sleep(0.5)
pyautogui.typewrite('notepad\n', interval=0.05)
time.sleep(0.5)
pyautogui.typewrite('Hello from automation!\nThis file was saved by PyAutoGUI.', interval=0.02)
time.sleep(0.5)
pyautogui.hotkey('ctrl', 's')
time.sleep(0.5)
pyautogui.typewrite('automation_note.txt\n', interval=0.02)
pyautogui.press('enter')
PyAutoGUI can move the mouse, click, type, take screenshots, and locate on-screen images. It’s great for small tasks but brittle for large production automation — prefer APIs where possible. See GUI automation tutorials for more details and safety tips.
7. Example 5 — Scheduling scripts (schedule library + cron)
You can run scripts on a timetable with:
- Lightweight Python schedulers (for within a running process): schedule library.
- System schedulers: cron (Linux/macOS) or Task Scheduler (Windows).
Using schedule:
# scheduled_task.py
import schedule, time
from subprocess import run
def job():
run(['python', 'fetch_api.py'])
schedule.every().day.at("08:00").do(job)
while True:
schedule.run_pending()
time.sleep(60)
Better for production: Use system cron or a process manager (systemd) to run scripts at fixed times and capture logs. Combining schedule with a long-running process is handy for small setups, but a cron job is more robust for single-run scripts.
8. Best practices, safety and debugging tips
- Use virtual environments per project to manage dependencies.
- Add logging (logging module) rather than print() for production scripts.
- Handle exceptions and retries for network or I/O operations (use try/except and backoff strategies).
- Respect remote resources (rate limits, robots.txt, Terms of Service).
- Test locally and back up data before running scripts that modify or delete files.
- Version control everything with Git so you can roll back changes.
9. Where to go next (resources & recommended reading)
- Automate the Boring Stuff with Python — Al Sweigart (excellent beginner book, project-based). Automate the Boring Stuff
- Real Python — excellent tutorials for Python fundamentals, terminal usage, and web scraping.
- DataCamp / tutorial roundups — comprehensive articles about Python automation workflows. DataCamp
- Stack Overflow Developer Survey — to understand language trends and community resources. survey.stackoverflow.co
Final checklist before you run: (do these)
- Back up any files that your script will modify.
- Run scripts first in a test folder or with a –dry-run flag.
- Add logging and capture errors to a file.
- If scraping, check robots.txt and throttle requests.
- Use credentials securely (environment variables, not hardcoded).
Short examples recap (copy-paste)
- batch_rename.py — rename files with pathlib.
- fetch_api.py — get JSON and save CSV with requests.
- scrape_headlines.py — basic BeautifulSoup scraping.
- gui_automate.py — PyAutoGUI keyboard/mouse automation.
scheduled_task.py — run daily with schedule (or use cron).



