
23 Python Scripting Techniques for Automation
Python is an ideal language for automation scripting due to its simplicity, flexibility, and extensive libraries. In this article, we’ll explore 23 essential techniques for automating various tasks using Python.
Table of Contents
- Executing System Commands
- Working with Files and Directories
- Manipulating Text Files
- Parsing JSON Data
- Handling CSV Data
- Working with Excel Spreadsheets
- Sending Emails
- Creating GUI Applications
- Using Web Scraping Techniques
- Interacting with Databases
- Using Regular Expressions
- Working with Dates and Times
- Creating CSV Reports
- Generating PDF Documents
- Using Selenium for Web Automation
- Handling XML Data
- Working with HTTP Requests and Responses
- Creating SSH Connections
- Using Paramiko for SSH Automation
- Working with SFTP Connections
- Handling ZIP Archives
- Creating PDF Reports from Excel Spreadsheets
- Using Scrapy for Large-Scale Web Scraping
1. Executing System Commands
Python provides the subprocess
module to execute system commands. You can use it like this:
“`python
import subprocess
subprocess.run([“ls”, “-l”])
“`
This will run the ls -l
command and display its output.
2. Working with Files and Directories
You can use the following techniques to work with files and directories:
- Listing Directory Contents: Use the
os.listdir()
function to list the contents of a directory. - Checking if a File Exists: Use the
os.path.exists()
function to check if a file exists. - Reading a File: Use the
open()
function in read mode ('r'
) to read the contents of a file.
“`python
import os
List directory contents
print(os.listdir(“/path/to/directory”))
Check if a file exists
if os.path.exists(“file.txt”):
print(“File exists”)
else:
print(“File does not exist”)
Read a file
with open(“file.txt”, “r”) as f:
print(f.read())
“`
3. Manipulating Text Files
You can use the following techniques to manipulate text files:
- Writing to a File: Use the
open()
function in write mode ('w'
) to write to a file. - Appending to a File: Use the
open()
function in append mode ('a'
) to append to a file.
“`python
import os
Write to a file
with open(“file.txt”, “w”) as f:
f.write(“Hello, World!”)
Append to a file
with open(“file.txt”, “a”) as f:
f.write(“\nThis is an appended line.”)
“`
4. Parsing JSON Data
You can use the json
module to parse JSON data.
“`python
import json
data = ‘{“name”: “John Doe”, “age”: 30}’
parsed_data = json.loads(data)
print(parsed_data[“name”]) # Output: John Doe
“`
5. Handling CSV Data
You can use the csv
module to handle CSV data.
“`python
import csv
data = [“John Doe”, 30, “New York”]
with open(“file.csv”, “w”) as f:
writer = csv.writer(f)
writer.writerow(data)
Read from a CSV file
with open(“file.csv”, “r”) as f:
reader = csv.reader(f)
for row in reader:
print(row)
“`
6. Working with Excel Spreadsheets
You can use the openpyxl
library to work with Excel spreadsheets.
“`python
from openpyxl import Workbook
wb = Workbook()
ws1 = wb.active
ws1[‘A1’] = ‘Hello, World!’
Save the workbook
wb.save(‘example.xlsx’)
“`
7. Sending Emails
You can use the smtplib
library to send emails.
“`python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg[‘From’] = ‘your-email@example.com’
msg[‘To’] = ‘recipient-email@example.com’
msg[‘Subject’] = ‘Hello, World!’
body = ‘This is a test email.’
msg.attach(MIMEText(body, ‘plain’))
server = smtplib.SMTP(‘smtp.example.com’, 587)
server.starttls()
server.login(msg[‘From’], ‘your-password’)
text = msg.as_string()
server.sendmail(msg[‘From’], msg[‘To’], text)
server.quit()
“`
8. Creating GUI Applications
You can use the tkinter
library to create GUI applications.
“`python
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text=’Hello, World!’)
label.pack()
button = tk.Button(root, text=’Click Me’, command=lambda: print(‘Button clicked!’))
button.pack()
root.mainloop()
“`
9. Using Web Scraping Techniques
You can use the requests
library to scrape websites.
“`python
import requests
url = ‘https://www.example.com’
response = requests.get(url)
Parse HTML content using BeautifulSoup
from bs4 import BeautifulSoup
soup = BeautifulSoup(response.text, ‘html.parser’)
print(soup.prettify())
“`
10. Interacting with Databases
You can use the sqlite3
library to interact with SQLite databases.
“`python
import sqlite3
conn = sqlite3.connect(‘example.db’)
Create a table
cursor = conn.cursor()
cursor.execute(”’
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER NOT NULL
)
”’)
Insert data into the table
cursor.execute(“INSERT INTO users (name, age) VALUES (‘John Doe’, 30)”)
conn.commit()
conn.close()
“`
11. Using Regular Expressions
You can use the re
library to work with regular expressions.
“`python
import re
text = ‘Hello, my name is John Doe and I am 30 years old.’
pattern = r’\b\w+\s*\w+\b’
matches = re.findall(pattern, text)
print(matches) # Output: [‘John’, ‘Doe’]
“`
12. Working with Dates and Times
You can use the datetime
library to work with dates and times.
“`python
from datetime import date, time, datetime
today = date.today()
print(today) # Output: Today’s date
now = datetime.now()
print(now) # Output: Current date and time
“`
13. Creating CSV Reports
You can use the csv
library to create CSV reports.
“`python
import csv
data = [
[‘Name’, ‘Age’],
[‘John Doe’, 30],
[‘Jane Doe’, 25]
]
with open(‘example.csv’, ‘w’) as f:
writer = csv.writer(f)
writer.writerows(data)
Read from the CSV file
with open(‘example.csv’, ‘r’) as f:
reader = csv.reader(f)
for row in reader:
print(row)
“`
14. Generating PDF Documents
You can use the fpdf
library to generate PDF documents.
“`python
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font(“Arial”, size=15)
text = “Hello, World!”
pdf.cell(200, 10, txt=text, ln=True, align=’L’)
pdf.output(“example.pdf”)
“`
15. Using Selenium for Web Automation
You can use the selenium
library to automate web interactions.
“`python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(‘https://www.example.com’)
print(driver.title) # Output: The title of the webpage
Find an element by its CSS selector
element = driver.find_element_by_css_selector(‘#my-element’)
print(element.text) # Output: The text within the element
Close the browser window
driver.quit()
“`
16. Handling XML Data
You can use the xml.etree.ElementTree
library to handle XML data.
“`python
import xml.etree.ElementTree as ET
data = ”’
”’
root = ET.fromstring(data)
print(root.tag) # Output: The root tag
for child in root:
print(child.tag, child.attrib) # Output: The tags and attributes of each person element
“`
17. Using the requests
Library for HTTP Requests
You can use the requests
library to send HTTP requests.
“`python
import requests
url = ‘https://www.example.com’
response = requests.get(url)
print(response.status_code) # Output: The status code of the response
print(response.text) # Output: The text content of the response
“`
18. Creating a REST API with Flask
You can use the flask
library to create a REST API.
“`python
from flask import Flask, jsonify
app = Flask(name)
@app.route(‘/api/endpoint’, methods=[‘GET’])
def get_data():
data = {‘key’: ‘value’}
return jsonify(data)
if name == ‘main‘:
app.run()
“`
19. Using the schedule
Library to Schedule Tasks
You can use the schedule
library to schedule tasks.
“`python
import schedule
import time
def job():
print(‘I am doing some work!’)
schedule.every(10).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)
“`
20. Creating a GUI Application with PyQt5
You can use the PyQt5
library to create a GUI application.
“`python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel
class MyWidget(QWidget):
def init(self):
super().init()
self.initUI()
def initUI(self):
layout = QVBoxLayout()
self.setLayout(layout)
label = QLabel('Hello, World!')
layout.addWidget(label)
button = QPushButton('Click Me')
layout.addWidget(button)
button.clicked.connect(self.on_button_click)
self.setWindowTitle('MyWidget')
self.show()
def on_button_click(self):
print('Button clicked!')
def main():
app = QApplication(sys.argv)
ex = MyWidget()
sys.exit(app.exec_())
if name == ‘main‘:
main()
“`
These examples demonstrate how to perform various tasks in Python, from simple programming concepts to complex applications and frameworks.