Automating System Tasks with Python

Loading

Automating system tasks with Python can help manage files, processes, and system operations efficiently. Python provides various built-in modules and external libraries to automate tasks like file handling, process management, network operations, and system monitoring.


Why Automate System Tasks?

Saves time on repetitive tasks
Reduces human errors
Improves productivity
Schedules tasks efficiently


1. Automating File and Folder Operations

Creating, Renaming, and Deleting Files & Folders

import os

# Create a new folder
os.makedirs("TestFolder", exist_ok=True)

# Rename a folder
os.rename("TestFolder", "RenamedFolder")

# Delete a folder
os.rmdir("RenamedFolder")

# Create a new file
with open("test_file.txt", "w") as file:
file.write("Hello, Automation!")

# Delete a file
os.remove("test_file.txt")

print("File operations completed!")

Now files and folders are managed automatically!


2. Automating System Commands

Running System Commands Using subprocess

import subprocess

# Run a system command (Windows)
subprocess.run("dir", shell=True)

# Run a system command (Linux/Mac)
subprocess.run("ls -la", shell=True)

Now system commands can be executed via Python!


3. Automating System Monitoring

Checking CPU and Memory Usage

import psutil

# Get CPU usage
cpu_usage = psutil.cpu_percent(interval=1)
print(f"CPU Usage: {cpu_usage}%")

# Get memory usage
memory = psutil.virtual_memory()
print(f"Memory Usage: {memory.percent}%")

Now system performance is monitored automatically!


4. Automating File Backup and Compression

Zipping Files for Backup

import shutil

# Compress folder into a ZIP file
shutil.make_archive("backup", "zip", "C:/Users/Username/Documents")
print("Backup created successfully!")

Now files are automatically backed up!


5. Automating Scheduled System Tasks

Scheduling Tasks with schedule

import schedule
import time

def clean_temp():
print("Cleaning temporary files...")

# Run the function every hour
schedule.every().hour.do(clean_temp)

while True:
schedule.run_pending()
time.sleep(60)

Now the task runs automatically every hour!


6. Automating Web Downloads

Downloading Files Using requests

import requests

url = "https://example.com/sample.pdf"
response = requests.get(url)

with open("downloaded_file.pdf", "wb") as file:
file.write(response.content)

print("File downloaded successfully!")

Now files are downloaded automatically!


7. Automating Email Notifications

Sending Emails Using smtplib

import smtplib

sender_email = "your_email@example.com"
receiver_email = "recipient@example.com"
password = "yourpassword"

message = "Subject: Automation Alert\n\nTask Completed Successfully!"

with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)

print("Email sent successfully!")

Now email notifications are automated!

Leave a Reply

Your email address will not be published. Required fields are marked *