24 Python Scripting Techniques for Automation
Python is an ideal language for automation due to its simplicity, readability, and extensive libraries. With over 24 techniques listed below, you can automate a wide range of tasks, from file management to web scraping.
1. File Management
- Use
osmodule to interact with the file system. - Create directories:
os.mkdir("directory_name") - List files in a directory:
os.listdir("directory_name") - Delete files:
os.remove("file_name.txt")
2. Regular Expressions
- Use
remodule for pattern matching and manipulation. - Find all occurrences of a pattern in a string:
re.findall(pattern, string) - Replace occurrences of a pattern with another string:
re.sub(pattern, replacement, string)
3. Email Automation
- Send emails using
smtplib:smtpObj = smtplib.SMTP("smtp.gmail.com") - Compose email:
msg = MIMEMultipart() - Attach files:
attachment = open('filename.txt', 'rb')
4. Web Scraping
- Use
requestsandBeautifulSoup:soup = BeautifulSoup(html_content, 'html.parser') - Find all occurrences of a tag in HTML:
soup.find_all('tag_name') - Extract text from HTML tags:
text = soup.get_text()
5. Data Manipulation
- Use
pandasfor data analysis and manipulation. - Read CSV file:
df = pd.read_csv('data.csv') - Write CSV file:
df.to_csv('output.csv', index=False)
6. Task Scheduling
- Schedule tasks using
schedule:schedule.every(10).minutes.do(task_function) - Run scheduled tasks:
while True: schedule.run_pending()
7. GUI Automation
- Create a GUI using
tkinter. - Use buttons and event handlers to interact with the GUI.
8. System Administration
- Use
psutilfor system administration tasks. - List running processes:
pslist = psutil.process_iter() - Kill a process:
process.terminate()
9. Excel Automation
- Read and write Excel files using
openpyxl. - Read Excel file:
wb = load_workbook('input.xlsx') - Write to Excel file:
ws['A1'] = 'Hello, World!'
10. PDF Generation
- Use
fpdffor PDF generation. - Create a new PDF document:
pdf = FPDF() - Add pages and text to the PDF.
11. Image Processing
- Use
Pillow(PIL) for image processing. - Open an image file:
img = Image.open('image.jpg') - Save an image file:
img.save('output.png')
12. Network Scanning
- Use
scapyfor network scanning and packet sniffing. - Scan a network range:
scan = conf.Lprange("192.168.1.", "255") - Print scan results:
for ip in scan: print(ip)
13. SSH Client
- Use
paramikofor SSH connections. - Connect to an SSH server:
ssh_client.connect('example.com', 22, 'username', 'password') - Run a command on the remote server:
stdin, stdout, stderr = ssh_client.exec_command(command)
14. FTP Client
- Use
ftplibfor FTP connections. - Connect to an FTP server:
ftp_client.connect('example.com', 21) - Download a file from the FTP server:
local_file = open('downloaded.txt', 'wb')
15. HTTP Client
- Use
requestsfor HTTP requests. - Send a GET request:
response = requests.get('https://example.com') - Send a POST request:
response = requests.post('https://example.com', data=data)
16. XML Parsing
- Use
xml.etree.ElementTreefor XML parsing and manipulation. - Parse an XML file:
root = ET.parse('input.xml').getroot() - Create a new XML element:
elem = ET.SubElement(root, 'element')
17. JSON Parsing
- Use
jsonmodule for JSON parsing and serialization. - Parse a JSON string:
data = json.loads(json_string) - Serialize data to JSON:
json_string = json.dumps(data)
18. CSV Parsing
- Use
csvmodule for CSV parsing and manipulation. - Parse a CSV file:
reader = csv.reader(open('input.csv', 'r')) - Write data to a CSV file:
writer = csv.writer(open('output.csv', 'w'))
19. Excel XLSX Parsing
- Use
openpyxlfor Excel XLSX parsing and manipulation. - Read an Excel XLSX file:
wb = load_workbook('input.xlsx') - Write data to an Excel XLSX file:
ws['A1'] = 'Hello, World!'
20. PDF Parsing
- Use
fpdffor PDF parsing and manipulation. - Read a PDF file:
pdf = FPDF('input.pdf') - Create a new PDF document:
new_pdf = FPDF()
21. Image XLSX Parsing
- Use
Pillow(PIL) for image XLSX parsing and manipulation. - Open an image file:
img = Image.open('image.jpg') - Save an image file:
img.save('output.png')
22. XML to JSON Conversion
- Use
xmltodictlibrary for XML to JSON conversion. - Convert an XML string to a JSON string:
json_data = xmltodict.parse(xml_string)
23. CSV to JSON Conversion
- Use
csv2jsonlibrary for CSV to JSON conversion. - Convert a CSV file to a JSON file:
json.dump(csv2json.convert(file), sys.stdout)
24. Excel XLSX to CSV Conversion
- Use
openpyxlandpandaslibraries for Excel XLSX to CSV conversion. - Convert an Excel XLSX file to a CSV file:
csv_file = pandas.read_excel('input.xlsx').to_csv('output.csv', index=False)
This concludes our list of 24 Python scripting techniques for automation. Mastering these techniques will enable you to automate various tasks, from simple file management to complex data analysis and manipulation.