Python offers many features and it is easy to learn, but, sometimes developers have to code repetitive tasks and snippets again and again. Thankfully, there are some of these tasks that can be automated, freeing up our time to concentrate on other tasks that truly require our time and effort.
Today, we will explore 10 Python Automation Scripts that will help you perform automation tasks across various aspects of development, and save up your precious time.
Automation in Python
Python automation means using the language to write scripts, programs, or other tools that carry out automatic processes without the need for human interaction. Automation can be used for several processes including file management, web scraping, data analysis, system monitoring, and more.
Here are the common automation tasks used in Python:
- File Management:
- Reading/Writing Files: Automate tasks such as reading from or writing to files.
- File Organization: Rename, move, or delete files based on certain criteria.
- Web Scraping:
- Data Extraction: Automate the process of collecting data from websites.
- Monitoring: Check websites for updates or changes.
- Data Processing:
- Data Cleaning: Automate the cleaning and preparation of data for analysis.
- Data Transformation: Convert data from one format to another.
- System Administration:
- Task Scheduling: Automate repetitive system tasks, such as backups or software updates.
- Resource Monitoring: Check system resources and log statistics.
- API Interaction:
- Data Retrieval: Automate interactions with APIs to fetch data.
- Automated Reporting: Collect data from various sources and generate reports.
- Testing:
- Unit Testing: Automate the testing of code to ensure it works as expected.
- Integration Testing: Test interactions between different parts of a system.
Thus, automation is a crucial aspect of programming with Python nowadays and has many applications and use cases across diverse fields.
How to perform Automation in Python?
Although it may seem difficult, you can automate an operation if you follow these instructions and have a basic understanding of Python. Writing an automation script is simple.
1. Identify the Task
- Define the problem: Understand what you want to automate.
- Break down the task: Divide it into smaller, manageable steps.
2. Choose the Right Tools and Libraries
Select appropriate libraries based on the task. Here are some common tasks and their corresponding libraries:
- File Management: os, shutil, glob, pathlib
- Web Scraping: requests, BeautifulSoup, Scrapy, Selenium
- Data Processing: pandas, numpy
- System Administration: subprocess, psutil, schedule
- API Interaction: requests, httpx
- Testing: unittest, pytest
3. Write the Script
Develop a Python script that uses the selected libraries to automate the task. Use the right libraries and functions while scripting.
4. Test the Script
Run the script in a controlled environment to ensure it works correctly and handles exceptions. Make sure you test it properly and figure out possible errors and issues.
5. Schedule the Script
Use scheduling tools or services to run your script at desired intervals. You can update and fix your code if it’s not functioning correctly or if there are some instances of incorrect outcomes. This phase allows you to add functionality to your code if you would like to in the future.
10 Python Automation Scripts To Boost Your Efficiency
These 10 Python automation scripts can be used to automate repetitive processes, as demonstrated by these amazing examples.
1. File Management: Organize Files by Extension
This script provides a straightforward way to organize files by their extensions. It’s easy to customize and extend for more complex file management needs. For more sophisticated file management tasks, you can consider incorporating additional libraries or writing more complex logic.
import os import shutil def organize_files_by_extension(directory): """ Organizes files in the given directory by their extensions. :param directory: Path to the directory to organize. """ # Iterate through all files in the directory for filename in os.listdir(directory): # Ignore directories if os.path.isdir(os.path.join(directory, filename)): continue # Extract file extension ext = filename.split('.')[-1] ext_dir = os.path.join(directory, ext) # Create a directory for the extension if it doesn't exist os.makedirs(ext_dir, exist_ok=True) # Construct full file paths src_path = os.path.join(directory, filename) dest_path = os.path.join(ext_dir, filename) # Move file to the new directory shutil.move(src_path, dest_path) print(f'Moved {filename} to {ext_dir}') # Example usage organize_files_by_extension('/path/to/your/directory')
Here’s how you can run the script:
Install Python: Ensure you have Python installed on your system.
Save the Script: Save the script to a .py file, e.g., organize_files.py.
Run the Script: Open a terminal and run the script with the directory path you want to organize.
python organize_files.py
Thus, this script helps organize files in a directory by moving them into folders named after their file extensions.
2. Web Scraping: Extract Titles from a Web Page
This script provides a simple way to scrape and extract article titles from a web page. It can be extended to handle more complex scenarios such as dealing with dynamic content or saving data in different formats. For web scraping projects, always check the target website’s robots.txt file and terms of service to ensure compliance with their policies.
import requests from bs4 import BeautifulSoup def extract_article_titles(url): """ Extracts article titles from a web page. :param url: URL of the web page to scrape. :return: List of article titles. """ try: # Fetch the content of the URL response = requests.get(url) response.raise_for_status() # Raise an HTTPError if the HTTP request returned an unsuccessful status code except requests.RequestException as e: print(f"Error fetching the URL: {e}") return [] # Parse the HTML content soup = BeautifulSoup(response.content, 'html.parser') # Extract article titles titles = [] for header in soup.find_all(['h1', 'h2', 'h3']): # Adjust tags as needed title = header.get_text(strip=True) if title: # Only add non-empty titles titles.append(title) return titles # Example usage url = 'https://example.com/articles' titles = extract_article_titles(url) for i, title in enumerate(titles, start=1): print(f'{i}. {title}')
Here’s how you can run the script:
Save the Script: Save the script as extract_titles.py.
Run the Script: Open a terminal and run the script with Python:
python extract_titles.py
This is a Python automation script for web scraping that extracts article titles from a web page using the requests and BeatifulSoup libraries. Web scraping involves fetching and parsing the HTML content of web pages to extract useful data.
3. System Administration: Check Disk Usage
This Python automation script designed for system administration that checks disk usage. This script monitors the available disk space on a system and alerts if the free space falls below a specified threshold. It provides a basic yet effective way to monitor disk usage.
import shutil import os import platform def check_disk_usage(threshold): """ Checks the disk usage and alerts if free space is below the given threshold. :param threshold: The minimum free space percentage allowed before an alert is raised. """ # Get disk usage statistics total, used, free = shutil.disk_usage('/') # Calculate percentage of free space free_percent = free / total * 100 # Print the disk usage details print(f"Total: {total / (1024**3):.2f} GB") print(f"Used: {used / (1024**3):.2f} GB") print(f"Free: {free / (1024**3):.2f} GB ({free_percent:.2f}%)") # Check if free space is below the threshold if free_percent < threshold: print(f"Warning: Only {free_percent:.2f}% free space left!") else: print(f"Sufficient disk space available ({free_percent:.2f}% free).") # Example usage threshold = 20 # Set threshold for free space percentage check_disk_usage(threshold)
Here’s how you can run the script:
Save the Script: Save the script as check_disk_usage.py.
Run the Script: Open a terminal and run the script with Python:
python check_disk_usage.py
By customizing and extending this script, you can create a more robust system administration tool that suits your specific needs. Always consider additional features like logging, notifications, and multi-platform support based on your use case.
4. API Interaction: Fetch Weather Data
Now let’s explore a Python automation script that fetches weather data from an API. This script uses an API to get current weather information for a specified location and displays the details.
It provides a foundation for various enhancements, including handling multiple cities, saving data, using command-line arguments, and creating a GUI. Always ensure that the API key is kept secure and comply with the terms of service of the weather API provider.
import requests def fetch_weather(api_key, city): """ Fetches and prints weather data for a given city using OpenWeatherMap API. :param api_key: Your OpenWeatherMap API key. :param city: City name to fetch the weather for. """ # API endpoint url = 'http://api.openweathermap.org/data/2.5/weather' # Parameters to be sent with the request params = { 'q': city, 'appid': api_key, 'units': 'metric' # Use 'imperial' for Fahrenheit } try: # Send GET request response = requests.get(url, params=params) response.raise_for_status() # Raise an HTTPError for bad responses # Parse JSON response data = response.json() # Extract weather information city_name = data['name'] weather = data['weather'][0]['description'] temp = data['main']['temp'] feels_like = data['main']['feels_like'] humidity = data['main']['humidity'] wind_speed = data['wind']['speed'] # Print weather information print(f"Weather in {city_name}:") print(f" Description: {weather}") print(f" Temperature: {temp}°C") print(f" Feels Like: {feels_like}°C") print(f" Humidity: {humidity}%") print(f" Wind Speed: {wind_speed} m/s") except requests.RequestException as e: print(f"Error fetching weather data: {e}") except KeyError as e: print(f"Error parsing weather data: {e}") # Example usage api_key = 'your_api_key_here' # Replace with your OpenWeatherMap API key city = 'London' fetch_weather(api_key, city)
Here’s how you can run the script:
Save the Script: Save the script as fetch_weather.py.
Run the Script: Open a terminal and run the script with Python:
python fetch_weather.py
5. Data Processing: Clean a CSV File
Let’s walk through a Python automation script that processes and cleans a CSV file. This script will handle common cleaning tasks such as removing missing values, converting data types, renaming columns, and filtering rows.
import pandas as pd def clean_csv(input_file, output_file): """ Cleans a CSV file by removing missing values, converting data types, renaming columns, and filtering rows. :param input_file: Path to the input CSV file. :param output_file: Path to the output cleaned CSV file. """ # Load CSV data into DataFrame df = pd.read_csv(input_file) # Print original data summary print("Original Data Summary:") print(df.info()) print(df.head(), "\n") # Remove rows with missing values df.dropna(inplace=True) # Convert data types (example: converting 'date' to datetime) # Ensure to adjust the column names and types as per your CSV if 'date' in df.columns: df['date'] = pd.to_datetime(df['date'], errors='coerce') # Rename columns (example: renaming 'old_name' to 'new_name') # Ensure to adjust the column names as per your CSV df.rename(columns={'old_name': 'new_name'}, inplace=True) # Filter rows (example: keep only rows where 'column' > value) # Adjust the filter conditions as per your needs if 'column' in df.columns: df = df[df['column'] > value] # Print cleaned data summary print("Cleaned Data Summary:") print(df.info()) print(df.head(), "\n") # Save cleaned data to new CSV file df.to_csv(output_file, index=False) print(f"Cleaned data saved to {output_file}") # Example usage input_file = 'data.csv' output_file = 'cleaned_data.csv' clean_csv(input_file, output_file)
Here’s how you can run the script:
Save the Script: Save the script as clean_csv.py.
Prepare CSV Files: Ensure you have the input CSV file (e.g., data.csv).
Run the Script: Open a terminal and run the script with Python.
python clean_csv.py
This Python script provides a framework for cleaning CSV files, which is a common task in data processing workflows. By extending and customizing it, you can adapt the script to fit specific data cleaning needs. The use of pandas allows for powerful and flexible data manipulation, making it ideal for such tasks.
6. Battery Indicator
Creating a Python automation script for a battery indicator typically involves reading the battery status and providing feedback through an appropriate interface, such as a console output, GUI application, or physical indicator (like an LED).
Here’s a simple Python Automation Script that reads the battery status from a system file and prints the status to the console. This can serve as a basis for more complex applications involving GUIs or hardware interfacing.
import time def read_battery_status(file_path): """ Reads the battery status from the specified file. :param file_path: Path to the battery status file. :return: Battery charge level as an integer percentage. """ try: with open(file_path, 'r') as file: capacity = file.read().strip() return int(capacity) except (FileNotFoundError, ValueError) as e: print(f"Error reading battery status: {e}") return None def print_battery_status(charge_level): """ Prints the battery status to the console. :param charge_level: Current battery charge level as an integer percentage. """ if charge_level is not None: print(f"Battery level: {charge_level}%") # Example usage BATTERY_FILE_PATH = '/sys/class/power_supply/BAT0/capacity' # Example path for Linux try: while True: charge_level = read_battery_status(BATTERY_FILE_PATH) print_battery_status(charge_level) time.sleep(60) # Check every 60 seconds except KeyboardInterrupt: print("Exiting...")
Here’s how you can run the script:
Save the Script: Save the script as battery_indicator.py.
Run the Script: Open a terminal and run the script with Python:
python battery_indicator.py
This Python script provides a foundational example of how to automate battery status monitoring and feedback. It can be adapted and extended based on specific needs such as integrating with GUIs, triggering alerts, or interfacing with physical indicators. Always consider the platform-specific details and permissions required when accessing system files or hardware resources.
7. Convert PDF to Image
Converting PDF files to images using Python can be achieved using various libraries. One popular choice is PyMuPDF (also known as fitz), which allows you to extract pages from a PDF and save them as images.
Below is a Python script that uses PyMuPDF to convert each page of a PDF file to an image file (PNG format). This script will read a PDF file, extract each page as an image, and save it.
import fitz # PyMuPDF def pdf_to_images(pdf_file, output_folder): """ Converts each page of a PDF file to an image (PNG). :param pdf_file: Path to the PDF file. :param output_folder: Path to the output folder where images will be saved. """ # Open the PDF file pdf_document = fitz.open(pdf_file) # Iterate through each page of the PDF for page_num in range(len(pdf_document)): # Get the page page = pdf_document.load_page(page_num) # Convert page to image (PNG) image = page.get_pixmap() # Save image to output folder image_path = f"{output_folder}/page_{page_num + 1}.png" image.save(image_path) print(f"Page {page_num + 1} saved as {image_path}") # Close the PDF document pdf_document.close() # Example usage if __name__ == "__main__": pdf_file = "example.pdf" # Replace with your PDF file path output_folder = "output_images" # Replace with your desired output folder # Create output folder if it doesn't exist import os if not os.path.exists(output_folder): os.makedirs(output_folder) # Convert PDF to images pdf_to_images(pdf_file, output_folder)
Here’s how you can use it:
Prepare your PDF file:
Place the PDF file that you want to convert into the same directory as your Python script. For this example, let’s assume your PDF file is named ‘example.pdf’.
Copy the Script:
Copy the Python script provided earlier into a new file. You can use any text editor or an integrated development environment (IDE) such as Visual Studio Code, PyCharm, or IDLE.
Modify Script Parameters (Optional)
- Replace ‘example.pdf’ with the name of your PDF file.
- Modify ‘output_images’ to the desired name of the output folder where images will be saved.
Run the script:
Open a terminal or command prompt and navigate to the directory where your Python script ‘pdf_to_image.py’ and ‘example.pdf’ are located. Run the script by typing:
python pdf_to_image.py
This script provides a basic framework for converting PDF files to images using Python and PyMuPDF. Depending on your requirements, you can extend it further to include additional functionalities such as image format conversion, page range selection, or integration with GUI frameworks for user interaction.
8. Email Scheduler
Creating an email scheduler using Python involves automating the process of sending emails at scheduled intervals or specific times. This can be accomplished by combining Python’s smtplib for sending emails and schedule or datetime for scheduling tasks.
This script will allow you to communicate with your contacts easily and say good-bye to sending emails by hand. Emails can be automatically scheduled and sent at a specified time with this script.
import smtplib import schedule import time from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from datetime import datetime def send_email(subject, body, to_email): """ Sends an email with the specified subject and body. :param subject: Subject of the email. :param body: Body content of the email. :param to_email: Recipient email address. """ from_email = "[email protected]" # Replace with your email address password = "your_password" # Replace with your email password # Create the email content message = MIMEMultipart() message['From'] = from_email message['To'] = to_email message['Subject'] = subject message.attach(MIMEText(body, 'plain')) try: # Connect to the SMTP server server = smtplib.SMTP('smtp.example.com', 587) # Replace with your SMTP server and port server.starttls() # Secure the connection server.login(from_email, password) # Send the email server.send_message(message) print(f"Email sent to {to_email} at {datetime.now()}") # Disconnect from the server server.quit() except Exception as e: print(f"Failed to send email: {e}") def schedule_email(): """ Schedules the email to be sent. """ subject = "Scheduled Email" body = "This is an automatically scheduled email." to_email = "[email protected]" # Replace with recipient email address send_email(subject, body, to_email) # Schedule the email to be sent every day at a specific time schedule_time = "14:00" # Replace with your desired schedule time (24-hour format) schedule.every().day.at(schedule_time).do(schedule_email) print(f"Email scheduler started. Will send emails every day at {schedule_time}.") try: while True: # Run pending scheduled tasks schedule.run_pending() time.sleep(1) except KeyboardInterrupt: print("Email scheduler stopped.")
Here’s how you can run the script:
Save the Script: Save the script as email_scheduler.py.
Edit the Script:
- Replace “[email protected]” and “your_password” with your actual email and password.
- Replace “smtp.example.com” with your SMTP server address and port (e.g., smtp.gmail.com, port 587 for Gmail).
- Replace “[email protected]” with the recipient’s email address.
- Modify the schedule_time as needed.
Run the Script: Open a terminal or command prompt, navigate to the directory containing email_scheduler.py, and run:
python email_scheduler.py
By following these steps, you can set up a Python script to schedule and send emails automatically. This can be extended with more sophisticated scheduling logic, handling multiple recipients, or using templated email content.
9. Image Optimizer
This Python automation script lets you easily edit and modify your images like an expert with only a few clicks, eliminating the need for pricey programmes or sophisticated editing equipment.
This script optimises photos by use the well-liked Pillow module. To crop, resize, flip, rotate, compress, blur, sharpen, alter brightness and contrast, and add filters to an image, it makes use of the Python Imaging Library (PIL).
from PIL import Image import os def optimize_image(input_path, output_path, quality=85, max_width=None, max_height=None): """ Optimize an image by resizing and adjusting its quality. :param input_path: Path to the input image file. :param output_path: Path to save the optimized image file. :param quality: Image quality for the output (1 to 100). :param max_width: Maximum width for resizing (optional). :param max_height: Maximum height for resizing (optional). """ try: # Open an image file with Image.open(input_path) as img: print(f"Optimizing {input_path}...") # Resize the image if max_width or max_height is specified if max_width or max_height: img.thumbnail((max_width, max_height), Image.ANTIALIAS) # Save the image with the specified quality img.save(output_path, optimize=True, quality=quality) print(f"Saved optimized image to {output_path}") except Exception as e: print(f"Error optimizing {input_path}: {e}") def optimize_images_in_directory(directory, output_directory, quality=85, max_width=None, max_height=None): """ Optimize all images in the specified directory. :param directory: Directory containing images to optimize. :param output_directory: Directory to save optimized images. :param quality: Image quality for the output (1 to 100). :param max_width: Maximum width for resizing (optional). :param max_height: Maximum height for resizing (optional). """ # Create the output directory if it doesn't exist if not os.path.exists(output_directory): os.makedirs(output_directory) # Iterate through all files in the directory for filename in os.listdir(directory): input_path = os.path.join(directory, filename) output_path = os.path.join(output_directory, filename) # Check if the file is an image if filename.lower().endswith(('png', 'jpg', 'jpeg', 'gif', 'bmp')): optimize_image(input_path, output_path, quality, max_width, max_height) # Example usage if __name__ == "__main__": input_dir = "input_images" # Replace with your input directory output_dir = "optimized_images" # Replace with your output directory image_quality = 85 # Adjust quality as needed (1 to 100) max_img_width = 800 # Replace with max width or None max_img_height = 600 # Replace with max height or None optimize_images_in_directory(input_dir, output_dir, quality=image_quality, max_width=max_img_width, max_height=max_img_height)
Here’s how you can run the script:
Save the Script: Save the script as image_optimizer.py.
Prepare Directories: Create an input_images directory and place your images there. Ensure the script has the correct paths for input_dir and output_dir.
Run the Script: Open a terminal or command prompt, navigate to the directory containing image_optimizer.py, and run:
python image_optimizer.py
This Python script provides a straightforward way to optimize images by resizing and adjusting their quality. It’s flexible and can be easily adapted to different requirements or integrated into larger systems.
10. Video Optimizer
Creating a Python automation script for video optimization involves compressing video files to reduce their size while maintaining quality. This is useful for reducing storage requirements, improving streaming performance, and speeding up file transfers.
Below is an example using the ffmpeg tool, which is a powerful multimedia framework for handling video, audio, and other multimedia files and streams.
import os import subprocess def optimize_video(input_path, output_path, resolution="1280x720", bitrate="1M"): """ Optimize a video file using ffmpeg. :param input_path: Path to the input video file. :param output_path: Path to save the optimized video file. :param resolution: Resolution for the output video (e.g., '1280x720'). :param bitrate: Bitrate for the output video (e.g., '1M' for 1 Mbps). """ try: print(f"Optimizing {input_path}...") # Construct the ffmpeg command command = [ 'ffmpeg', '-i', input_path, '-vf', f'scale={resolution}', '-b:v', bitrate, '-c:a', 'copy', output_path ] # Run the command subprocess.run(command, check=True) print(f"Saved optimized video to {output_path}") except subprocess.CalledProcessError as e: print(f"Error optimizing {input_path}: {e}") def optimize_videos_in_directory(directory, output_directory, resolution="1280x720", bitrate="1M"): """ Optimize all videos in the specified directory. :param directory: Directory containing videos to optimize. :param output_directory: Directory to save optimized videos. :param resolution: Resolution for the output videos (e.g., '1280x720'). :param bitrate: Bitrate for the output videos (e.g., '1M' for 1 Mbps). """ # Create the output directory if it doesn't exist if not os.path.exists(output_directory): os.makedirs(output_directory) # Iterate through all files in the directory for filename in os.listdir(directory): input_path = os.path.join(directory, filename) output_path = os.path.join(output_directory, filename) # Check if the file is a video if filename.lower().endswith(('mp4', 'mkv', 'mov', 'avi', 'flv', 'wmv', 'webm')): optimize_video(input_path, output_path, resolution, bitrate) # Example usage if __name__ == "__main__": input_dir = "input_videos" # Replace with your input directory output_dir = "optimized_videos" # Replace with your output directory video_resolution = "1280x720" # Replace with desired resolution (e.g., '1920x1080') video_bitrate = "1M" # Replace with desired bitrate (e.g., '500K' for 500 kbps) optimize_videos_in_directory(input_dir, output_dir, resolution=video_resolution, bitrate=video_bitrate)
Here’s how you can run the script:
Save the Script: Save the script as video_optimizer.py.
Prepare Directories: Create an input_videos directory and place your video files there. Ensure the script has the correct paths for input_dir and output_dir.
Run the Script: Open a terminal or command prompt, navigate to the directory containing video_optimizer.py, and run:
python video_optimizer.py
This will optimize videos from the input_videos directory and save the optimized versions in the optimized_videos directory.
This Python script provides a straightforward way to optimize videos using ffmpeg. It’s flexible and can be easily adapted to different requirements or integrated into larger automation workflows. You can modify it further to include additional features like logging, more complex optimizations, or support for batch processing larger collections of videos.
Conclusion
These Python Automation Scripts have been designed on a variety of factors to give you the best automation experience across your daily tasks and everyday projects.