2679 words
13 minutes
10 Useful Python Snippets for Everyday Automation Tasks

Python Snippets for Everyday Automation Tasks#

Automation streamlines repetitive computer-based actions, freeing up time and reducing potential errors. Python is a widely adopted language for automation due to its clear syntax, extensive standard library, and vast ecosystem of third-party packages. These features enable the creation of scripts that perform tasks ranging from file management and data processing to web interactions and sending notifications. Understanding and utilizing small, focused pieces of code, known as snippets, accelerates the development of these automation scripts. This article presents 10 practical Python snippets applicable to common everyday automation needs.

Why Python for Automation?#

Python’s suitability for automation stems from several factors:

  • Readability: The syntax is clean and easy to understand, making scripts maintainable.
  • Standard Library: It includes modules for numerous tasks without needing external installations, such as handling files, dates, and network communication.
  • Extensive Ecosystem: The Python Package Index (PyPI) hosts hundreds of thousands of third-party libraries for specialized automation needs (e.g., web scraping with BeautifulSoup, data analysis with pandas, interacting with APIs using requests).
  • Cross-Platform Compatibility: Python scripts generally run on Windows, macOS, and Linux without modification.

These characteristics make Python an accessible yet powerful tool for automating tasks across various domains.

10 Useful Python Snippets for Everyday Automation Tasks#

This section details 10 practical Python code snippets designed for common automation scenarios, providing code, explanations, and example applications.

1. Reading and Writing Text Files#

Handling text files is fundamental to many automation tasks, such as reading configuration files, processing logs, or generating reports. Python’s built-in open() function provides a simple interface for file operations.

# Writing to a file
file_content_write = "This is a line of text.\nAnother line."
with open("example_output.txt", "w") as file:
file.write(file_content_write)
# Reading from a file
file_content_read = ""
with open("example_output.txt", "r") as file:
file_content_read = file.read()
print("File content read:")
print(file_content_read)

Explanation:

  • The open() function opens a file. The second argument specifies the mode: "w" for writing (creates the file if it doesn’t exist, overwrites if it does) and "r" for reading.
  • The with open(...) as file: syntax is recommended because it ensures the file is automatically closed even if errors occur.
  • file.write() writes the specified string to the file.
  • file.read() reads the entire content of the file into a string. Other methods include file.readline() for reading one line or file.readlines() for reading all lines into a list.

Example Application: Reading a list of tasks from a text file, processing them, and writing results to another file.

2. Working with File Paths and Directories#

Managing files involves constructing paths, checking existence, and listing contents. The os.path and os modules provide essential functions for these tasks.

import os
# Get the current working directory
current_directory = os.getcwd()
print(f"Current Directory: {current_directory}")
# Construct a file path
file_name = "my_document.txt"
full_path = os.path.join(current_directory, "data", file_name) # Creates path like '.../data/my_document.txt'
print(f"Constructed Path: {full_path}")
# Check if a path exists
path_to_check = "example_output.txt" # From snippet 1
exists = os.path.exists(path_to_check)
print(f"'{path_to_check}' exists: {exists}")
# List files and directories in a path
items_in_current_dir = os.listdir(current_directory)
print(f"Items in current directory: {items_in_current_dir[:5]}...") # Print first 5

Explanation:

  • os.getcwd() returns the path to the directory where the script is currently running.
  • os.path.join() intelligently combines path components using the appropriate separator for the operating system (e.g., \ on Windows, / on Linux/macOS), preventing errors.
  • os.path.exists(path) checks if a file or directory at path exists.
  • os.listdir(path) returns a list of names of the entries (files and directories) in the directory given by path.

Example Application: Creating a script that iterates through files in a specific directory, processes them, and moves them to an archive folder.

3. Creating Directories#

Automation often requires organizing output or processing files within specific folder structures. The os module offers a straightforward way to create new directories.

import os
# Define the path for the new directory
new_directory_path = "reports/daily_summary"
# Create the directory (including any necessary parent directories)
try:
os.makedirs(new_directory_path, exist_ok=True)
print(f"Directory '{new_directory_path}' created or already exists.")
except OSError as e:
print(f"Error creating directory {new_directory_path}: {e}")

Explanation:

  • os.makedirs(path, exist_ok=True) creates all directories specified in path.
  • The exist_ok=True argument is crucial for automation scripts; it prevents an error if the target directory already exists. Without it, the script would fail on subsequent runs.
  • A try...except block is included to catch potential OSError exceptions, such as permission issues, making the script more robust.

Example Application: Setting up a folder structure (e.g., output/year/month/day) before saving daily generated reports.

4. Copying and Moving Files#

Manipulating files is a common automation task. The shutil module provides higher-level file operations than the basic functions in os.

import shutil
import os
# Create dummy files for demonstration
if not os.path.exists("source_file.txt"):
with open("source_file.txt", "w") as f:
f.write("Content to be copied or moved.")
if not os.path.exists("destination_folder"):
os.makedirs("destination_folder")
# Define source and destination paths
source_file = "source_file.txt"
destination_copy = "destination_folder/copied_file.txt"
destination_move = "destination_folder/moved_file.txt"
try:
# Copy a file
shutil.copy2(source_file, destination_copy)
print(f"'{source_file}' copied to '{destination_copy}'")
# Move a file (note: source_file will be removed after move)
# Ensure source file exists for the move demo if copy was run first
if os.path.exists(source_file):
shutil.move(source_file, destination_move)
print(f"'{source_file}' moved to '{destination_move}'")
else:
print(f"'{source_file}' not found for move operation.")
except FileNotFoundError:
print("Error: Source file or destination directory not found.")
except Exception as e:
print(f"An error occurred: {e}")

Explanation:

  • shutil.copy2(src, dst) copies the file located at src to the location dst. copy2 attempts to preserve metadata like timestamp.
  • shutil.move(src, dst) moves the file or directory located at src to the location dst. If dst is an existing directory, the source is moved inside it.
  • try...except blocks handle potential errors like the source file not existing.

Example Application: Moving processed files to an archive directory, copying template files to a new project folder, or backing up important documents.

5. Finding Files with Patterns#

Locating files based on patterns (like extensions or parts of the name) is a frequent need. The glob module finds pathnames matching a specified pattern.

import glob
import os
# Create dummy files for demonstration
if not os.path.exists("data"):
os.makedirs("data")
with open("data/report_2023.csv", "w") as f: pass
with open("data/log_2024_01.txt", "w") as f: pass
with open("data/report_2024.csv", "w") as f: pass
with open("data/config.ini", "w") as f: pass
# Find all .csv files in the 'data' directory
csv_files = glob.glob("data/*.csv")
print(f"Found CSV files: {csv_files}")
# Find all files starting with 'log_' in the 'data' directory
log_files = glob.glob("data/log_*.txt")
print(f"Found log files: {log_files}")
# Find all files in 'data' directory (similar to os.listdir, but supports patterns)
all_data_files = glob.glob("data/*")
print(f"All files in data: {all_data_files}")

Explanation:

  • glob.glob(pattern) returns a list of pathnames that match pattern.
  • Patterns use standard Unix shell rules:
    • * matches any sequence of characters.
    • ? matches any single character.
    • [seq] matches any character in seq.
    • [!seq] matches any character not in seq.
  • The path can include directory components.

Example Application: Finding all log files from a specific date, locating all image files of a certain type (.jpg, .png), or identifying all configuration files (.ini, .yaml).

6. Processing CSV Data#

Comma Separated Values (CSV) files are a common format for exchanging data. The csv module provides convenient functions for reading and writing CSV files, correctly handling delimiters and quoted fields.

import csv
# Sample CSV data (as a string for demo, typically read from a file)
csv_data = """name,age,city
Alice,30,New York
Bob,25,London
Charlie,35,Paris
"""
# Write data to a CSV file
data_to_write = [
['Product ID', 'Name', 'Price'],
['P101', 'Laptop', '1200'],
['P102', 'Keyboard', '75']
]
with open("products.csv", "w", newline="") as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerows(data_to_write)
print("Data written to products.csv")
# Read data from a CSV file
print("\nReading products.csv:")
with open("products.csv", "r", newline="") as csvfile:
csv_reader = csv.reader(csvfile)
for row in csv_reader:
print(row)
# Reading into dictionaries (more useful for headers)
print("\nReading products.csv as dictionaries:")
with open("products.csv", "r", newline="") as csvfile:
csv_dict_reader = csv.DictReader(csvfile)
for row_dict in csv_dict_reader:
print(row_dict)

Explanation:

  • open(..., newline="") is important when working with CSV files to prevent extra blank rows caused by different operating system newline conventions.
  • csv.writer(file) creates an object to write data to the file.
  • csv_writer.writerows(list_of_rows) writes multiple rows from a list of lists.
  • csv.reader(file) creates an object to read data from the file, yielding rows as lists of strings.
  • csv.DictReader(file) reads rows as dictionaries, using the first row as keys (headers). This is often more convenient for accessing data by column name.

Example Application: Reading customer data from a CSV for processing, parsing log files formatted as CSV, or generating reports in CSV format.

7. Processing JSON Data#

JavaScript Object Notation (JSON) is a lightweight data interchange format widely used for data transmission on the web and in configuration files. Python’s built-in json module handles encoding and decoding JSON data.

import json
# Sample Python dictionary
data_to_json = {
"name": "Example Project",
"version": "1.0",
"enabled": True,
"tags": ["automation", "python"],
"config": {"timeout_seconds": 30, "retries": 3}
}
# Convert Python dictionary to JSON string
json_string = json.dumps(data_to_json, indent=4) # indent for readability
print("Python dict converted to JSON string:")
print(json_string)
# Write JSON string to a file
with open("config.json", "w") as json_file:
json.dump(data_to_json, json_file, indent=4)
print("\nPython dict written to config.json")
# Read JSON from a string
json_string_from_api = '{"status": "success", "count": 100, "data": [1, 2, 3]}'
data_from_json_string = json.loads(json_string_from_api)
print("\nJSON string converted to Python dict:")
print(data_from_json_string)
print(f"Status: {data_from_json_string['status']}")
# Read JSON from a file
print("\nReading config.json:")
with open("config.json", "r") as json_file:
data_from_json_file = json.load(json_file)
print(data_from_json_file)

Explanation:

  • json.dumps(obj, indent=4) serializes a Python object (obj) into a JSON formatted string. indent adds formatting for human readability.
  • json.dump(obj, file, indent=4) serializes a Python object (obj) and writes it directly to a file object (file).
  • json.loads(string) deserializes a JSON formatted string into a Python object (typically a dictionary or list).
  • json.load(file) deserializes JSON data from a file object (file).

Example Application: Parsing API responses, reading configuration settings from a JSON file, or storing complex data structures for later use.

8. Making HTTP Requests#

Many automation tasks involve interacting with web services or APIs to fetch data, send commands, or check status. The requests library is the de facto standard for making HTTP requests in Python. While not part of the standard library, its ubiquity makes it essential for web-based automation.

import requests
# Make a simple GET request
url = "https://jsonplaceholder.typicode.com/posts/1" # A public test API
try:
response = requests.get(url)
# Check if the request was successful (status code 200)
response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx)
# Get data as JSON
data = response.json()
print("GET request successful:")
print(f"Status Code: {response.status_code}")
print(f"Response Body (JSON): {data}")
print(f"Title: {data['title']}")
except requests.exceptions.RequestException as e:
print(f"An error occurred during the HTTP request: {e}")
# Example POST request (simulated)
post_url = "https://jsonplaceholder.typicode.com/posts"
new_post_data = {
"title": "foo",
"body": "bar",
"userId": 1
}
try:
post_response = requests.post(post_url, json=new_post_data) # Use json= for sending JSON data
post_response.raise_for_status()
created_post = post_response.json()
print("\nPOST request successful:")
print(f"Status Code: {post_response.status_code}")
print(f"Created Resource: {created_post}") # Includes the new ID assigned by the API
except requests.exceptions.RequestException as e:
print(f"An error occurred during the POST request: {e}")

Explanation:

  • requests.get(url) sends an HTTP GET request to the specified URL.
  • requests.post(url, json=data) sends an HTTP POST request with data as the JSON body.
  • The response object contains details about the response, including status_code and text (raw response body) or json() (if the response is JSON).
  • response.raise_for_status() is a convenient way to check for errors; it will raise an exception if the status code indicates a client or server error.
  • Error handling with try...except requests.exceptions.RequestException is important for robust scripts that interact with the network.

Example Application: Fetching data from a public API (weather, stock prices), interacting with internal business application APIs, or posting data to a web service.

9. Sending Simple Emails#

Automated scripts often need to send notifications or reports via email. Python’s smtplib and email modules handle sending emails using the Simple Mail Transfer Protocol (SMTP).

import smtplib
from email.mime.text import MIMEText
import ssl # For secure connection
# Email details
sender_email = "your_email@example.com" # Replace with your sender email
receiver_email = "recipient@example.com" # Replace with recipient email
password = "your_email_password" # Replace with your email password (use app passwords for security)
subject = "Automated Report"
body = "This is an automated email containing the daily report."
# Create the email message
message = MIMEText(body)
message["Subject"] = subject
message["From"] = sender_email
message["To"] = receiver_email
# Connect to SMTP server and send email
smtp_server = "smtp.gmail.com" # Replace with your SMTP server address
port = 465 # Port for SSL (or 587 for TLS)
try:
# Secure connection with SSL
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string())
print("Email sent successfully!")
except smtplib.SMTPAuthenticationError:
print("Failed to send email: Authentication Error. Check username and password.")
except ConnectionRefusedError:
print("Failed to send email: Connection Refused. Check SMTP server address and port.")
except Exception as e:
print(f"An error occurred while sending email: {e}")
# NOTE: Sending emails requires correct SMTP server details and credentials.
# Using app-specific passwords is recommended over using your main password.

Explanation:

  • email.mime.text.MIMEText(body) creates a basic email message object with plain text.
  • Setting message["Subject"], message["From"], and message["To"] populates the email headers.
  • smtplib.SMTP_SSL() creates a secure connection to the SMTP server using SSL. smtplib.SMTP() can be used with starttls() for TLS.
  • context = ssl.create_default_context() sets up a default SSL context for security.
  • server.login(sender_email, password) authenticates with the SMTP server.
  • server.sendmail(from_addr, to_addrs, msg) sends the email. to_addrs can be a list of recipients.
  • Error handling is crucial as email sending can fail due to network issues, incorrect credentials, or server problems.

Example Application: Sending daily summaries of task completion, alerting administrators to errors, or distributing automated reports to a mailing list.

10. Handling Dates and Times#

Many automation tasks are time-sensitive or involve processing data based on timestamps. The datetime module provides classes for working with dates, times, and time intervals.

from datetime import datetime, timedelta, date, time
# Get the current date and time
now = datetime.now()
print(f"Current datetime: {now}")
# Get the current date
today = date.today()
print(f"Current date: {today}")
# Create a specific datetime object
specific_dt = datetime(2024, 3, 10, 14, 30, 0) # Year, month, day, hour, minute, second
print(f"Specific datetime: {specific_dt}")
# Format datetime objects into strings
formatted_dt = now.strftime("%Y-%m-%d %H:%M:%S") # YYYY-MM-DD HH:MM:SS
print(f"Formatted datetime: {formatted_dt}")
# Parse strings into datetime objects
date_string = "2023-12-25"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d").date()
print(f"Parsed date string: {parsed_date}")
# Perform date arithmetic
tomorrow = today + timedelta(days=1)
print(f"Tomorrow's date: {tomorrow}")
yesterday = today - timedelta(days=1)
print(f"Yesterday's date: {yesterday}")
one_hour_later = now + timedelta(hours=1)
print(f"One hour from now: {one_hour_later}")

Explanation:

  • datetime.now() returns the current date and time as a datetime object.
  • date.today() returns the current date as a date object.
  • datetime(year, month, day, ...) creates a datetime object for a specific point in time.
  • strftime(format) formats a datetime object into a string according to the specified format codes (e.g., %Y for year, %m for month, %d for day, %H for hour, %M for minute, %S for second).
  • datetime.strptime(string, format) parses a string into a datetime object based on the provided format.
  • timedelta objects represent a duration and can be added to or subtracted from date or datetime objects.

Example Application: Naming log files with the current date, calculating deadlines, processing data based on date ranges, or scheduling tasks to run at specific times (though scheduling itself typically uses external tools like cron or the Windows Task Scheduler, which trigger Python scripts that use datetime for logic).

Real-World Applications & Considerations#

These snippets serve as building blocks for more complex automation workflows. For instance, an automation script could:

  1. Use Snippet 8 (requests) to download a data file from a web server.
  2. Use Snippet 3 (os.makedirs) to create a timestamped directory (using Snippet 10 datetime) to store the file.
  3. Use Snippet 4 (shutil.move) to move the downloaded file into the new directory.
  4. Use Snippet 6 (csv) or Snippet 7 (json) to read and process the file content.
  5. Use Snippet 1 (open()) to write processed results to a new report file.
  6. Use Snippet 9 (smtplib) to email the report file as an attachment (requires using email.mime.multipart and email.mime.base in addition to MIMEText).

For production-level automation, incorporating robust error handling (try...except blocks) is crucial. Scripts might also benefit from logging (using the logging module) to record their activity and any issues encountered. Dependencies on external libraries like requests should be managed using tools like pip and documented in a requirements.txt file.

Key Takeaways#

  • Python’s readability and extensive libraries make it highly suitable for automating everyday tasks.
  • Small code snippets can perform specific, common automation actions like file manipulation, data processing, and communication.
  • The standard library provides modules for file I/O (open, os, shutil, glob), structured data (csv, json), and dates/times (datetime).
  • Widely used external libraries like requests are essential for web-based automation tasks.
  • Combining these snippets enables the creation of more complex and powerful automation workflows.
  • Implementing error handling and proper dependency management (pip, requirements.txt) contributes to building reliable automation scripts.
  • These Python snippets offer a practical starting point for automating repetitive computer tasks, increasing efficiency and accuracy.
10 Useful Python Snippets for Everyday Automation Tasks
https://dev-resources.site/posts/10-useful-python-snippets-for-everyday-automation-tasks/
Author
Dev-Resources
Published at
2025-06-29
License
CC BY-NC-SA 4.0