Introduction to Senior-Level Python Scripts
As a Python developer, you're likely familiar with the basics of the language. However, to take your skills to the next level and impress your colleagues, you need to start writing more complex and useful scripts. In this article, we'll explore nine Python scripts that will make you look like a senior developer. These scripts cover a range of topics, from file automation to web scraping, and are designed to be practical and easy to understand.
1. File Automation Script
The first script is a file automation tool that organizes your files into folders based on their extensions. This is a classic senior-level task that demonstrates your ability to work with files and directories.
import os
import shutil
# Define a dictionary to map file extensions to folders
folder_mapping = {
'Documents': ['.txt', '.docx', '.pdf'],
'Images': ['.jpg', '.png', '.gif'],
'Videos': ['.mp4', '.avi', '.mov']
}
# Get the current directory
current_dir = os.getcwd()
# Iterate over all files in the current directory
for filename in os.listdir(current_dir):
# Get the file extension
file_ext = os.path.splitext(filename)[1]
# Check if the file extension is in the folder mapping
for folder, extensions in folder_mapping.items():
if file_ext in extensions:
# Move the file to the corresponding folder
shutil.move(os.path.join(current_dir, filename), os.path.join(current_dir, folder, filename))
break
What makes this script senior-level is the use of a dictionary to map file extensions to folders, and the ability to iterate over all files in the current directory.
2. API Wrapper Script
The second script is an API wrapper that retrieves data from the GitHub API. This demonstrates your ability to work with APIs and handle JSON data.
import requests
# Define the API endpoint and parameters
endpoint = 'https://api.github.com/users/octocat'
params = {
'accept': 'application/json'
}
# Send a GET request to the API endpoint
response = requests.get(endpoint, params=params)
# Check if the response was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()
print(data)
else:
print('Failed to retrieve data')
What makes this script senior-level is the use of the requests library to send a GET request to the API endpoint, and the ability to handle JSON data.
3. Data Processing Script
The third script is a data processing tool that calculates the average value of a list of numbers. This demonstrates your ability to work with data and perform calculations.
import statistics
# Define a list of numbers
numbers = [1, 2, 3, 4, 5]
# Calculate the average value
average = statistics.mean(numbers)
# Print the average value
print(f'The average value is: {average}')
What makes this script senior-level is the use of the statistics library to calculate the average value, and the ability to work with data.
4. CLI Tool Script
The fourth script is a CLI tool that generates a random password. This demonstrates your ability to create interactive tools and work with user input.
import random
import string
# Define a function to generate a random password
def generate_password(length):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for _ in range(length))
return password
# Get the password length from the user
length = int(input('Enter the password length: '))
# Generate and print the password
password = generate_password(length)
print(f'Your random password is: {password}')
What makes this script senior-level is the use of a function to generate the random password, and the ability to work with user input.
5. Web Scraping Helper Script
The fifth script is a web scraping helper that extracts all links from a webpage. This demonstrates your ability to work with HTML and parse web pages.
import requests
from bs4 import BeautifulSoup
# Define the webpage URL
url = 'https://www.example.com'
# Send a GET request to the webpage
response = requests.get(url)
# Parse the HTML response
soup = BeautifulSoup(response.text, 'html.parser')
# Find all links on the webpage
links = soup.find_all('a')
# Print the links
for link in links:
print(link.get('href'))
What makes this script senior-level is the use of the requests and BeautifulSoup libraries to send a GET request and parse the HTML response, and the ability to extract links from the webpage.
6. System Utility Script
The sixth script is a system utility that checks if a process is running. This demonstrates your ability to work with system resources and check process status.
import psutil
# Define the process name
process_name = 'chrome.exe'
# Iterate over all running processes
for proc in psutil.process_iter(['pid', 'name']):
# Check if the process name matches
if proc.info['name'] == process_name:
print(f'The process {process_name} is running with PID {proc.info["pid"]}')
break
else:
print(f'The process {process_name} is not running')
What makes this script senior-level is the use of the psutil library to iterate over all running processes, and the ability to check process status.
7. File Comparison Script
The seventh script is a file comparison tool that checks if two files are identical. This demonstrates your ability to work with files and compare their contents.
import filecmp
# Define the two files to compare
file1 = 'file1.txt'
file2 = 'file2.txt'
# Compare the files
if filecmp.cmp(file1, file2):
print(f'The files {file1} and {file2} are identical')
else:
print(f'The files {file1} and {file2} are not identical')
What makes this script senior-level is the use of the filecmp library to compare the files, and the ability to work with files.
8. Network Utility Script
The eighth script is a network utility that checks if a website is reachable. This demonstrates your ability to work with network resources and check website status.
import requests
# Define the website URL
url = 'https://www.example.com'
# Send a GET request to the website
try:
response = requests.get(url)
if response.status_code == 200:
print(f'The website {url} is reachable')
else:
print(f'The website {url} is not reachable')
except requests.exceptions.RequestException as e:
print(f'The website {url} is not reachable: {e}')
What makes this script senior-level is the use of the requests library to send a GET request to the website, and the ability to handle exceptions.
9. Backup Script
The ninth script is a backup tool that creates a zip archive of a directory. This demonstrates your ability to work with files and directories, and create backups.
import zipfile
import os
# Define the directory to backup
directory = 'example_directory'
# Create a zip archive
with zipfile.ZipFile('backup.zip', 'w') as zip_file:
# Iterate over all files in the directory
for root, dirs, files in os.walk(directory):
for file in files:
# Add the file to the zip archive
zip_file.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), directory))
What makes this script senior-level is the use of the zipfile library to create a zip archive, and the ability to work with files and directories.
Conclusion
These nine Python scripts demonstrate a range of senior-level skills, from file automation to web scraping, and from API wrappers to system utilities. By working through these examples, you'll be able to improve your Python skills and create more complex and useful scripts. Whether you're a beginner or an intermediate developer, these scripts are designed to be practical and easy to understand. So why not get started today and see what you can create?
To learn more about Python and stay up-to-date with the latest developments, be sure to follow me for more articles and tutorials. Happy coding!
Found this useful? Follow me on Dev.to for more Python automation tips every week. Drop a comment below — I reply to every one!
喜欢这篇文章?关注获取更多Python自动化内容!
🔒 Want More?
This article covers the basics. In Content Creator Ultimate Bundle (Save 33%) ($29.99), you get:
- Complete source code
- Advanced techniques
- Real-world examples
- Step-by-step tutorials
- Bonus templates
Top comments (0)