DEV Community

qing
qing

Posted on

Automate Your Daily Tasks with Python: 15 Real Examples

Automate Your Daily Tasks with Python: 15 Real Examples

As a developer, you're likely no stranger to the concept of automation. Automating repetitive tasks can save you a significant amount of time and increase your productivity. Python, with its extensive range of libraries and easy-to-learn syntax, is an ideal language for automating daily tasks. In this article, we'll explore 15 real-world examples of automating daily tasks with Python.

1. Sending Automated Emails

You can use Python's smtplib library to send automated emails. This can be useful for sending reminders, notifications, or even automated reports.

import smtplib
from email.mime.text import MIMEText

# Define the email parameters
subject = "Automated Email"
body = "This is an automated email sent using Python."
from_email = "your_email@gmail.com"
to_email = "recipient_email@gmail.com"

# Create a text message
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email

# Send the email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_email, "your_password")
server.sendmail(from_email, to_email, msg.as_string())
server.quit()
Enter fullscreen mode Exit fullscreen mode

2. Renaming Files in Bulk

If you have a large number of files that need to be renamed, Python's os library can help. You can write a script to rename files based on a specific pattern or prefix.

import os

# Define the directory and prefix
directory = "/path/to/directory"
prefix = "new_prefix_"

# Rename the files
for i, filename in enumerate(os.listdir(directory)):
    if filename.endswith(".txt"):
        new_filename = f"{prefix}{i+1}.txt"
        os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
Enter fullscreen mode Exit fullscreen mode

3. Data Entry Automation

Python's pyautogui library can be used to automate data entry tasks. You can write a script to fill out forms, click buttons, and perform other repetitive tasks.

import pyautogui
import time

# Wait for 5 seconds to switch to the correct window
time.sleep(5)

# Fill out the form
pyautogui.typewrite("John Doe")
pyautogui.press("tab")
pyautogui.typewrite("johndoe@example.com")
pyautogui.press("enter")
Enter fullscreen mode Exit fullscreen mode

4. Web Scraping

Python's requests and beautifulsoup4 libraries can be used to scrape data from websites. You can write a script to extract specific data and save it to a file.

import requests
from bs4 import BeautifulSoup

# Send a GET request to the website
response = requests.get("https://www.example.com")

# Parse the HTML content
soup = BeautifulSoup(response.content, "html.parser")

# Extract the data
data = soup.find_all("h2")

# Save the data to a file
with open("data.txt", "w") as file:
    for item in data:
        file.write(item.text + "\n")
Enter fullscreen mode Exit fullscreen mode

5. Backup Files

Python's shutil library can be used to backup files. You can write a script to copy files from one directory to another.

import shutil
import os

# Define the source and destination directories
source_dir = "/path/to/source/directory"
dest_dir = "/path/to/destination/directory"

# Backup the files
for filename in os.listdir(source_dir):
    shutil.copy2(os.path.join(source_dir, filename), dest_dir)
Enter fullscreen mode Exit fullscreen mode

6. Schedule Tasks

Python's schedule library can be used to schedule tasks. You can write a script to run a specific task at a certain time or interval.

import schedule
import time

# Define the task
def job():
    print("Task executed")

# Schedule the task to run every day at 8am
schedule.every().day.at("08:00").do(job)

# Run the scheduler
while True:
    schedule.run_pending()
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

7. Automate Social Media Posts

Python's tweepy library can be used to automate social media posts. You can write a script to post updates to Twitter or other social media platforms.

import tweepy

# Define the API keys
consumer_key = "your_consumer_key"
consumer_secret = "your_consumer_secret"
access_token = "your_access_token"
access_token_secret = "your_access_token_secret"

# Authenticate with the API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Post an update
api = tweepy.API(auth)
api.update_status("Hello, world!")
Enter fullscreen mode Exit fullscreen mode

8. Monitor System Resources

Python's psutil library can be used to monitor system resources. You can write a script to track CPU usage, memory usage, or disk usage.

import psutil

# Get the current CPU usage
cpu_usage = psutil.cpu_percent()

# Get the current memory usage
memory_usage = psutil.virtual_memory().percent

# Print the usage
print(f"CPU usage: {cpu_usage}%")
print(f"Memory usage: {memory_usage}%")
Enter fullscreen mode Exit fullscreen mode

9. Automate File Transfers

Python's ftplib library can be used to automate file transfers. You can write a script to upload or download files from an FTP server.


python
import ftplib

# Define the FTP server and credentials
ftp_server = "ftp.example.com"
username = "your
Enter fullscreen mode Exit fullscreen mode

Top comments (0)