10 Useful Python Automation Scripts for File Management, Data Retrieval, and System Monitoring
This article presents ten practical Python scripts that automate tasks such as backing up files, downloading data, renaming and processing CSV files, monitoring network status, sending emails, synchronizing directories, scraping web pages, analyzing logs, and scheduling recurring jobs.
1. File Backup Script This script periodically copies all files from a specified source directory to a destination directory, appending a timestamp to each backup file name.
import shutil
import os
import datetime
def backup_files(src, dst):
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
for filename in os.listdir(src):
shutil.copy(os.path.join(src, filename), os.path.join(dst, f"{timestamp}_{filename}"))
if __name__ == "__main__":
src_dir = "/path/to/source"
dst_dir = "/path/to/destination"
backup_files(src_dir, dst_dir)2. Data Download Script A simple script that fetches a file from a given URL and saves it locally.
import requests
def download_file(url, path):
response = requests.get(url)
with open(path, 'wb') as file:
file.write(response.content)
if __name__ == "__main__":
url = "http://example.com/data.csv"
download_file(url, "/path/to/downloaded/file.csv")3. File Renaming Script Batch renames files in a directory by adding a specified prefix.
import os
def rename_files(directory, prefix="new_prefix"):
for filename in os.listdir(directory):
new_name = f"{prefix}_{filename}"
os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))
if __name__ == "__main__":
directory = "/path/to/directory"
rename_files(directory)4. CSV Data Processing Script Reads a CSV file, adds a new column whose values are derived from an existing column, and writes the result to a new file.
import csv
def process_csv(input_path, output_path):
with open(input_path, newline='') as csvfile:
reader = csv.DictReader(csvfile)
fieldnames = reader.fieldnames + ['new_column']
with open(output_path, 'w', newline='') as outfile:
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for row in reader:
row['new_column'] = row['column_name'] * 2
writer.writerow(row)
if __name__ == "__main__":
input_path = "/path/to/input.csv"
output_path = "/path/to/output.csv"
process_csv(input_path, output_path)5. Network Monitoring Script Checks the reachability of a host (default 8.8.8.8) using a ping command and reports the network status.
import subprocess
def check_network(host="8.8.8.8", port=53):
result = subprocess.run(["ping", "-c", "1", host], capture_output=True, text=True)
if result.returncode == 0:
print("Network is up.")
else:
print("Network is down.")
if __name__ == "__main__":
check_network()6. Automated Email Sending Script Sends an email with a subject and message body using an SMTP server.
import smtplib
from email.mime.text import MIMEText
def send_email(subject, message, to_addr, from_addr, password):
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
server = smtplib.SMTP('smtp.example.com')
server.starttls()
server.login(from_addr, password)
server.send_message(msg)
server.quit()
if __name__ == "__main__":
subject = "Daily Report"
message = "This is the daily report."
to_addr = "[email protected]"
from_addr = "[email protected]"
password = "password"
send_email(subject, message, to_addr, from_addr, password)7. Directory Synchronization Script Deletes the destination directory (if it exists) and copies the entire source directory to it, effectively synchronizing their contents.
import shutil
def sync_directories(src, dst):
shutil.rmtree(dst, ignore_errors=True)
shutil.copytree(src, dst)
if __name__ == "__main__":
src_dir = "/path/to/source"
dst_dir = "/path/to/destination"
sync_directories(src_dir, dst_dir)8. Webpage Scraping Script Fetches a webpage and returns its prettified HTML using BeautifulSoup.
import requests
from bs4 import BeautifulSoup
def fetch_webpage(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
return soup.prettify()
if __name__ == "__main__":
url = "http://example.com"
webpage_content = fetch_webpage(url)
print(webpage_content)9. Log Analysis Script Scans a log file for lines matching a given regular expression pattern (e.g., "ERROR") and prints matching lines.
import re
def analyze_logs(logfile, pattern):
with open(logfile, 'r') as file:
for line in file:
if re.search(pattern, line):
print(line.strip())
if __name__ == "__main__":
logfile = "/path/to/logfile.log"
pattern = r"ERROR"
analyze_logs(logfile, pattern)10. Scheduled Task Script Runs an infinite loop that executes a placeholder task every hour, demonstrating a simple scheduling mechanism.
import time
def scheduled_task():
while True:
# Your task here
print("Executing task...")
time.sleep(3600) # Sleep for an hour
if __name__ == "__main__":
scheduled_task()Test Development Learning Exchange
Test Development Learning Exchange
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.