os
: File and directory operations.shutil
: File management.schedule
: Task scheduling.subprocess
: Running system commands.selenium
: Automating browsers.pyautogui
: Controlling the keyboard/mouse.pandas
: Data processing.openpyxl
: Automating Excel tasks.```python import os import shutil
os.mkdir('example_folder')
shutil.move('source.txt', 'destination_folder/')
os.remove('old_file.txt') ```
openpyxl
```python from openpyxl import Workbook
wb = Workbook() ws = wb.active ws.title = "Data"
ws.append(["Name", "Age", "Country"]) ws.append(["Alice", 25, "USA"])
wb.save("example.xlsx") ```
```python import smtplib from email.mime.text import MIMEText
smtp_server = "smtp.gmail.com" port = 587 sender_email = "[email protected]" password = "your_password"
msg = MIMEText("Hello, this is an automated email.") msg['Subject'] = "Automation Test" msg['From'] = sender_email msg['To'] = "[email protected]"
server = smtplib.SMTP(smtp_server, port) server.starttls() server.login(sender_email, password) server.sendmail(sender_email, "[email protected]", msg.as_string()) server.quit() ```
schedule
```python import schedule import time
def job(): print("Running a scheduled task!")
schedule.every(10).seconds.do(job)
while True: schedule.run_pending() time.sleep(1) ```
for file in os.listdir('downloads'): if file.endswith('.pdf'): shutil.move(f'downloads/{file}', 'pdfs/') ```
sqlite3
to store and retrieve data.
```python
import sqlite3conn = sqlite3.connect('data.db') c = conn.cursor()
Create a table c.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)")
Insert data c.execute("INSERT INTO users (name) VALUES ('Alice')") conn.commit()
Retrieve data c.execute("SELECT * FROM users") print(c.fetchall())
conn.close() ```
Automatically move files into folders based on their type. ```python import os import shutil
file_types = {'Documents': ['.pdf', '.docx'], 'Images': ['.jpg', '.png']} source_dir = "downloads"
for file in os.listdir(source_dir): for folder, extensions in file_types.items(): if file.endswith(tuple(extensions)): os.makedirs(folder, exist_ok=True) shutil.move(os.path.join(source_dir, file), folder) ```
Scrape stock prices and save them in a CSV. ```python import requests from bs4 import BeautifulSoup import csv
response = requests.get("https://example.com/stocks") soup = BeautifulSoup(response.text, 'html.parser')
stocks = [] for row in soup.find_all('tr'): columns = row.find_all('td') stocks.append([col.text for col in columns])
with open('stocks.csv', 'w') as file: writer = csv.writer(file) writer.writerow(["Company", "Price", "Change"]) writer.writerows(stocks) ```
Fetch data from a database, generate an Excel report, and send it via email. ```python import pandas as pd from openpyxl import Workbook import smtplib from email.mime.text import MIMEText
data = {"Name": ["Alice", "Bob"], "Sales": [250, 300]} df = pd.DataFrame(data)
df.to_excel("daily_report.xlsx", index=False)
msg = MIMEText("Find today's sales report attached.") msg['Subject'] = "Daily Sales Report" msg['From'] = "[email protected]" msg['To'] = "[email protected]"
server = smtplib.SMTP("smtp.gmail.com", 587) server.starttls() server.login("[email protected]", "password") server.sendmail("[email protected]", "[email protected]", msg.as_string()) server.quit() ```
Login to a website and download a file. ```python from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com/login")
driver.find_element("name", "username").send_keys("my_user")
driver.find_element("name", "password").send_keys("my_password")
driver.find_element("name", "submit").click()
driver.find_element("link text", "Download Report").click()
driver.quit() ```
Send an alert if a webpage updates.
```python
import requests
import hashlib
url = "https://example.com"
response = requests.get(url)
current_hash = hashlib.md5(response.content).hexdigest()
if current_hash != previous_hash:
print("Page updated!")
```
try-except
blocks to catch errors.logging
to track automated processes.python
import logging
logging.basicConfig(filename='automation.log', level=logging.INFO)
logging.info("Task started!")
os.environ
for sensitive information like passwords.python
import os
password = os.environ.get("EMAIL_PASSWORD")