Python provides several modules and libraries that can automate repetitive tasks such as managing files, executing system commands, interacting with APIs, controlling browsers, operating desktop applications, scheduling tasks and working with spreadsheets.
In this article, we will explore the most useful Python modules and libraries for different types of automation and understand where each one fits.
1. os Module
It provides functions for interacting with the operating system. It can be used to automate tasks involving files, directories, environment variables and system information.
Common Uses:
- Create, rename and delete directories.
- Check whether files or folders exist.
- Read and modify environment variables.
- Get the current working directory.
- Automate operating-system-level tasks.
Example:
import os
folder = "reports"
if not os.path.exists(folder):
os.mkdir(folder)
print("Folder created:", folder)
Output
Folder created: reports
2. pathlib
This module provides an object-oriented way to work with filesystem paths. It makes path manipulation and file operations easier to read and maintain across operating systems.
Common uses:
- Create and inspect file paths.
- Check whether files exist.
- Find files inside directories.
- Create directories.
- Read and write files.
Example:
from pathlib import Path
folder = Path("reports")
folder.mkdir(exist_ok=True)
file_path = folder / "report.txt"
file_path.write_text("Automation completed.")
print(file_path.exists())
Output
True
3. shutil
It provides high-level operations for copying, moving, deleting and archiving files and directories.
Common uses:
- Move downloaded files into folders.
- Create backup copies.
- Copy complete directories.
- Delete temporary directories.
- Create archives.
Example:
import shutil
shutil.copy("report.txt", "backup_report.txt")
print("Backup created.")
Output
Backup created
4. glob
This module finds files and directories that match a specified pattern. It is useful when an automation script needs to process many files without manually specifying their names.
Common Uses:
- Find all CSV files.
- Locate all images in a folder.
- Process multiple PDF files.
- Search directories recursively.
Example:
import glob
files = glob.glob("reports/*.csv")
for file in files:
print(file)
Output
geek.csv
for.csv
geeks.csv
5. subprocess
This module allows Python programs to start and interact with external processes. It is useful when an automation workflow needs to execute commands or other programs.
Common Uses:
- Execute shell commands.
- Run other Python scripts.
- Start external applications.
- Capture command output.
- Automate command-line workflows.
Example:
import subprocess
result = subprocess.run(
["python", "--version"],
capture_output=True,
text=True
)
print(result.stdout or result.stderr)
Output
Python 3.13.0
6. Requests
Its a popular python library for sending HTTP requests. It is commonly used to automate interactions with REST APIs and web services.
Common Uses:
- Send GET and POST requests.
- Retrieve data from APIs.
- Upload files.
- Send form data.
- Automate API-based workflows.
Example:
import requests
response = requests.get("https://api.github.com")
if response.status_code == 200:
print("Request successful")
print(response.json())
7. BeautifulSoup
Its used for parsing HTML and XML documents. It can extract information from webpages after their content has been retrieved.
Common Uses:
- Extract data from HTML pages.
- Find links and tables.
- Collect text from webpage elements.
- Process structured webpage content.
Example:
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
print(soup.title.text)
8. Selenium
Used to automate web browsers. It can open webpages, locate elements, enter text, click buttons and perform other browser interactions.
Common uses:
- Automate form submission.
- Test web applications.
- Navigate through websites.
- Capture webpage information.
- Perform repetitive browser tasks.
Example:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
text_box = driver.find_element(By.NAME, "my-text")
text_box.send_keys("Hello")
driver.quit()
9. Playwright
A modern browser automation library that supports Chromium, Firefox and WebKit. Its Python API can be used for browser scripting, testing and other web automation tasks.
Common uses:
- Automate modern web applications.
- Fill forms and interact with webpages.
- Take screenshots.
- Automate multiple browsers.
- Run browser workflows using synchronous or asynchronous Python APIs.
Example:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://example.com")
print(page.title())
browser.close()
10. PyAutoGUI
PyAutoGUI allows Python programs to control the mouse and keyboard. It is useful when an application does not provide an API and browser-level automation is not sufficient.
Common uses:
- Click buttons automatically.
- Type text.
- Move the mouse.
- Take screenshots.
- Automate repetitive desktop actions.
Example:
import pyautogui
import time
time.sleep(2)
pyautogui.write("Hello from Python")
pyautogui.press("enter")
Output
Hello from Python
11. schedule
This library provides a simple way to execute Python functions at regular intervals or specific times.
Common uses:
- Run a report every day.
- Execute a task every hour.
- Send periodic notifications.
- Run regular data-processing jobs.
Example:
import schedule
import time
def run_task():
print("Task executed")
schedule.every().day.at("10:00").do(run_task)
while True:
schedule.run_pending()
time.sleep(1)
12. openpyxl
Its used to read and write Excel workbooks in the XLSX and XLSM formats. It is useful for automating spreadsheet-based business workflows.
Common uses:
- Create Excel reports.
- Update spreadsheet cells.
- Apply formatting.
- Read existing workbooks.
- Generate recurring reports.
Example:
from openpyxl import Workbook
workbook = Workbook()
sheet = workbook.active
sheet["A1"] = "Name"
sheet["B1"] = "Score"
sheet["A2"] = "John"
sheet["B2"] = 95
workbook.save("report.xlsx")
13. watchdog
Its a python library for monitoring changes in files and directories. It can trigger an action when a filesystem event occurs.
Common uses:
- Detect newly created files.
- Monitor modified files.
- Start processing when a file arrives.
- Build automatic file-processing pipelines.
Example:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
class Handler(FileSystemEventHandler):
def on_created(self, event):
print("New file:", event.src_path)
observer = Observer()
observer.schedule(Handler(), ".", recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()