-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
12.2.1 Automating repetitive tasks with Python scripts
One of the most powerful uses of Python is its ability to automate repetitive tasks. By writing Python scripts, you can save a significant amount of time and effort by handling tasks that would otherwise need to be done manually. This could include tasks like file management, web scraping, sending automated emails, data processing, and more.
Here’s a breakdown of how to automate common repetitive tasks with Python:
1. Automating File Management Tasks
One of the most common automation tasks is managing files—whether it’s renaming files, organizing files into directories, or moving files based on certain conditions.
Example 1: Renaming Files in a Directory
You can automate the process of renaming files in a directory. Let’s say you have a folder with images, and you want to add a prefix or suffix to all file names.
import os # Directory containing the files directory = '/path/to/your/folder/' # Loop through all files in the directory for filename in os.listdir(directory): if filename.endswith(".jpg"): new_name = "prefix_" + filename os.rename(os.path.join(directory, filename), os.path.join(directory, new_name)) print(f"Renamed: {filename} -> {new_name}")
This script renames all .jpg files by adding a "prefix_" to their original names.
Example 2: Moving Files Based on File Extension
Another automation task could be moving files of certain types to specific folders.
import shutil import os source_directory = '/path/to/source/folder' destination_directory = '/path/to/destination/folder' # Move all text files to another folder for filename in os.listdir(source_directory): if filename.endswith(".txt"): shutil.move(os.path.join(source_directory, filename), os.path.join(destination_directory, filename)) print(f"Moved: {filename}")
This script moves all .txt files from one directory to another.
2. Automating Web Scraping
Web scraping allows you to automate the process of extracting data from websites, which can be useful for gathering product prices, news, stock data, etc.
Example: Scraping Data from a Website
Using libraries like requests and BeautifulSoup, you can automate the extraction of data from HTML pages.
import requests from bs4 import BeautifulSoup # URL of the website url = 'https://example.com' # Get the page content response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # Extracting all headlines (assuming headlines are in <h2> tags) headlines = soup.find_all('h2') for headline in headlines: print(headline.text)
This script automates the task of extracting headlines from a webpage.
3. Automating Email Notifications
Automating the sending of email notifications can be helpful for alerting users about specific events, such as when a report is generated or when an error occurs in a task.
Example: Sending Automated Emails
Here’s how to send an email notification automatically using Python’s smtplib.
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # Email credentials and message details sender_email = 'your-email@example.com' receiver_email = 'receiver@example.com' password = 'your-email-password' # Setting up the MIME message message = MIMEMultipart() message['From'] = sender_email message['To'] = receiver_email message['Subject'] = 'Automated Email Notification' # Body of the email body = 'This is an automated email sent by Python script.' message.attach(MIMEText(body, 'plain')) # Sending the email try: server = smtplib.SMTP('smtp.example.com', 587) server.starttls() server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message.as_string()) print('Email sent successfully.') except Exception as e: print(f"Error: {e}") finally: server.quit()
This script sends an automated email with a specified message.
4. Automating Data Processing
You can automate the process of processing data, such as reading data from CSV files, performing calculations, and saving the results.
Example: Processing Data from a CSV File
Let’s say you have a CSV file with sales data, and you want to automate the process of calculating the total sales.
import pandas as pd # Read the CSV file data = pd.read_csv('sales_data.csv') # Calculate total sales total_sales = data['Sales'].sum() # Print the result print(f'Total Sales: {total_sales}')
This script automates the process of reading the data from a CSV file and calculating the total sales.
5. Automating System Administration Tasks
System administration tasks like checking disk usage, monitoring system performance, and cleaning up old log files can also be automated.
Example: Deleting Old Log Files
If you need to delete log files that are older than 30 days, you can use Python to automate this task.
import os import time log_directory = '/path/to/logs/' current_time = time.time() # Loop through the files in the directory for filename in os.listdir(log_directory): file_path = os.path.join(log_directory, filename) # Check if file is older than 30 days if os.path.getmtime(file_path) < current_time - 30 * 86400: os.remove(file_path) print(f'Deleted: {filename}')
This script deletes log files that have not been modified in the last 30 days.
6. Scheduling Automation Tasks
Once you’ve written your Python scripts to automate tasks, you might want them to run at regular intervals. You can schedule Python scripts to run automatically using:
- Windows Task Scheduler: To run a Python script at a specific time.
- Cron Jobs: For Linux/macOS to schedule tasks.
For example, on Linux, use crontab to schedule a script to run every day at 8 AM:
0 8 * * * /usr/bin/python3 /path/to/script.py
7. Conclusion
Automating repetitive tasks with Python scripts allows you to save time, reduce human error, and increase productivity. By automating tasks like file management, data processing, web scraping, and email notifications, you can focus on more strategic activities. Python's rich ecosystem of libraries and frameworks makes it an excellent choice for automating tasks across different domains.
Next Steps:
- Explore more Python libraries for automation, like selenium for browser automation or schedule for task scheduling.
- Build more complex workflows by combining multiple automated tasks.
Commenting is not enabled on this course.