10 Practical Python Automation Scripts to Simplify Everyday Tasks
This article introduces ten useful Python automation scripts—including file backup, batch renaming, email sending, text conversion, image processing, timed monitoring, web scraping, folder organization, note compilation, and screen capture—to help readers automate repetitive tasks and boost productivity.
Do you often face repetitive tasks such as text conversion, file organization, data scraping, or wish to automate daily operations like backing up files, sending emails, or taking screenshots? Python is an excellent choice for these needs. Below are ten practical automation scripts written in Python.
01 File Backup Script
This script automatically creates the target folder when backing up files, eliminating the need for manual folder creation.
import shutil, os
source_folder = r'C:\Users\example\folder'
target_folder = r'D:\backup\folder'
if not os.path.exists(target_folder):
os.makedirs(target_folder)
for foldername, subfolders, filenames in os.walk(source_folder):
for filename in filenames:
source_file = os.path.join(foldername, filename)
target_file = os.path.join(target_folder, filename)
shutil.copy(source_file, target_file)02 Batch File Renaming Script
This script quickly renames multiple files in a folder with a specified prefix.
import os
folder = r'C:\Users\example\folder'
prefix = 'prefix'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
new_file_path = os.path.join(folder, prefix + filename)
os.rename(file_path, new_file_path)03 Email Sending Script
This script sends an email with support for attachments and HTML content.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
USERNAME = '[email protected]'
PASSWORD = 'examplepassword'
RECEIVER = '[email protected]'
msg = MIMEMultipart()
msg['From'] = USERNAME
msg['To'] = RECEIVER
msg['Subject'] = 'Example Subject'
body = '<html><body><h1>Hello, World!</h1></body></html>'
msg.attach(MIMEText(body, 'html'))
with open('example.pdf', 'rb') as f:
attach = MIMEApplication(f.read(), _subtype='pdf')
attach.add_header('Content-Disposition', 'attachment', filename='example.pdf')
msg.attach(attach)
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(USERNAME, PASSWORD)
server.sendmail(USERNAME, RECEIVER, str(msg))
server.quit()04 Text Conversion Script
This script converts all words in a text file to lowercase.
with open('example.txt', 'r') as f:
text = f.read()
with open('example.txt', 'w') as f:
f.write(text.lower())05 Image Processing Script
This script converts images to another format (WebP) and compresses them.
from PIL import Image
import os
source_folder = r'C:\Users\example\folder'
target_folder = r'C:\Users\example\folder'
if not os.path.exists(target_folder):
os.makedirs(target_folder)
for filename in os.listdir(source_folder):
file_path = os.path.join(source_folder, filename)
if os.path.splitext(filename)[1] in ['.jpg', '.jpeg', '.png']:
img = Image.open(file_path)
img.save(os.path.join(target_folder, os.path.splitext(filename)[0] + '.webp'), 'webp', quality=50)06 Time Monitoring Script
This script periodically monitors a folder and backs up files at specified intervals.
import shutil, os, time
source_folder = r'C:\Users\example\folder'
target_folder = r'D:\backup\folder'
if not os.path.exists(target_folder):
os.makedirs(target_folder)
while True:
time.sleep(3600)
for foldername, subfolders, filenames in os.walk(source_folder):
for filename in filenames:
source_file = os.path.join(foldername, filename)
target_file = os.path.join(target_folder, time.strftime('%Y-%m-%d %H.%M.%S ') + filename)
shutil.copy(source_file, target_file)07 Website Data Scraping Script
This script fetches data from a specified website and saves the cleaned text to a file.
import requests
from bs4 import BeautifulSoup, Comment
url = 'https://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
with open('example.txt', 'w') as f:
for comment in soup.findAll(text=lambda text:isinstance(text, Comment)):
comment.extract()
f.write(soup.get_text())08 Folder Organization Script
This script automatically organizes files in a target folder by file type, creating subfolders as needed.
import os, shutil
source_folder = r'C:\Users\example\folder'
for filename in os.listdir(source_folder):
file_path = os.path.join(source_folder, filename)
if os.path.isfile(file_path):
folder_name = os.path.splitext(filename)[1][1:].upper()
if not os.path.exists(os.path.join(source_folder, folder_name)):
os.makedirs(os.path.join(source_folder, folder_name))
shutil.move(file_path, os.path.join(source_folder, folder_name, filename))09 Note Compilation Script
This script merges multiple Markdown files into a single PDF document.
import pypandoc
import os
source_folder = r'C:\Users\example\folder'
target_path = r'C:\Users\example
otes.pdf'
input_files = []
for filename in os.listdir(source_folder):
file_path = os.path.join(source_folder, filename)
if os.path.splitext(filename)[1] == '.md':
input_files.append(file_path)
output = pypandoc.convert_file(input_files, 'pdf', outputfile=target_path)10 Screenshot Script
This script captures the screen at regular intervals and saves the images to a designated folder.
from PIL import ImageGrab
import os, time
folder = r'C:\Users\example\folder'
if not os.path.exists(folder):
os.makedirs(folder)
while True:
time.sleep(3600)
im = ImageGrab.grab()
filename = os.path.join(folder, time.strftime('%Y-%m-%d %H.%M.%S ') + '.png')
im.save(filename)These ten practical automation scripts can help streamline work, study, and daily life by reducing processing time, minimizing errors, and improving efficiency.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
