IT Skills

Automating Tasks with Python





1. Basics of Task Automation with Python?

Why Automate with Python?

  • Reduce manual, repetitive tasks.
  • Improve efficiency and accuracy.
  • Handle large-scale operations quickly.

Libraries for Automation:

  • 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.

2. Common Automation Tasks & Examples

File and Directory Management

```python import os import shutil

Create a directory

os.mkdir('example_folder')

Move a file

shutil.move('source.txt', 'destination_folder/')

Delete a file

os.remove('old_file.txt') ```

Automating Excel with openpyxl

```python from openpyxl import Workbook

Create a new Excel file

wb = Workbook() ws = wb.active ws.title = "Data"

Add data

ws.append(["Name", "Age", "Country"]) ws.append(["Alice", 25, "USA"])

Save file

wb.save("example.xlsx") ```

Automating Emails

```python import smtplib from email.mime.text import MIMEText

Email setup

smtp_server = "smtp.gmail.com" port = 587 sender_email = "[email protected]" password = "your_password"

Create email

msg = MIMEText("Hello, this is an automated email.") msg['Subject'] = "Automation Test" msg['From'] = sender_email msg['To'] = "[email protected]"

Send email

server = smtplib.SMTP(smtp_server, port) server.starttls() server.login(sender_email, password) server.sendmail(sender_email, "[email protected]", msg.as_string()) server.quit() ```

Scheduling Tasks with schedule

```python import schedule import time

Define a task

def job(): print("Running a scheduled task!")

Schedule the task

schedule.every(10).seconds.do(job)

Run the scheduler

while True: schedule.run_pending() time.sleep(1) ```


3. Formulas & Techniques for Automation

File Operations:

  • Move files based on extensions: ```python import os import shutil

for file in os.listdir('downloads'): if file.endswith('.pdf'): shutil.move(f'downloads/{file}', 'pdfs/') ```

Database Automation:

  • Use sqlite3 to store and retrieve data. ```python import sqlite3

conn = 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() ```


4. Specific Automation Situations

Scenario 1: Organizing Files in a Directory

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) ```

Scenario 2: Web Scraping and Saving Data

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) ```

Scenario 3: Automating Daily Reports

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

Fetch data from a database (mocked here)

data = {"Name": ["Alice", "Bob"], "Sales": [250, 300]} df = pd.DataFrame(data)

Save to Excel

df.to_excel("daily_report.xlsx", index=False)

Email the report

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() ```

Scenario 4: Automating Web Actions with Selenium

Login to a website and download a file. ```python from selenium import webdriver

Set up WebDriver

driver = webdriver.Chrome()

Login

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()

Download a file

driver.find_element("link text", "Download Report").click()

driver.quit() ```

Scenario 5: Monitoring Websites for Changes

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!")
```


5. Best Practices for Automation

  • Error Handling: Use try-except blocks to catch errors.
  • Logs: Use logging to track automated processes.
    python
    import logging
    logging.basicConfig(filename='automation.log', level=logging.INFO)
    logging.info("Task started!")
  • Environment Variables: Use os.environ for sensitive information like passwords.
    python
    import os
    password = os.environ.get("EMAIL_PASSWORD")

6. Resources for Learning Automation

  • Books: "Automate the Boring Stuff with Python" by Al Sweigart.
  • Courses: Python automation courses on Udemy and Coursera.
  • Practice Tasks: Automate repetitive tasks you encounter daily, like organizing files or sending reports.

If you liked this, consider supporting us by checking out Tiny Skills - 250+ Top Work & Personal Skills Made Easy