How to automate tasks and build useful scripts with Python
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.
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}")
Use the requests library to fetch data automatically:
import requests
response = requests.get("https://api.github.com")
print(response.json())
Tools like Selenium allow Python to control a web browser:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
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)
You can run scripts automatically using:
scheduleimport schedule
import time
def job():
print("Running task...")
schedule.every(10).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)
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