Lesson 23: Introduction to Automation and Scripting

How to automate tasks and build useful scripts with Python

What Is Automation?

Automation means using code to perform repetitive tasks automatically. Python is one of the most popular languages for automation because it is simple, powerful, and has excellent libraries for interacting with files, systems, and the web.

Common Automation Use Cases

Basic Automation Script Example

Here’s a simple script that renames all .txt files in a folder:

import os

for i, filename in enumerate(os.listdir(".")):
    if filename.endswith(".txt"):
        new_name = f"file_{i}.txt"
        os.rename(filename, new_name)
        print(f"Renamed {filename} → {new_name}")

Automating Web Requests

Use the requests library to fetch data automatically:

import requests

response = requests.get("https://api.github.com")
print(response.json())

Automating Browser Actions

Tools like Selenium allow Python to control a web browser:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://example.com")

Working with CSV Files

Automating data processing is one of Python’s strengths:

import csv

with open("data.csv") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Scheduling Tasks

You can run scripts automatically using:

Example with schedule

import schedule
import time

def job():
    print("Running task...")

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

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

Best Practices for Automation

Next Steps

Now that you understand automation, you're ready to explore how Python interacts with operating systems in Lesson 24: Working with the OS and System Commands.

← Back to Lesson Index