9 PYTHON
SCRIPTS
WITH CODE
TO AUTOMATE YOUR WORK
ramchandrapadwal
Introduction
Python is a popular programming language
known for its simplicity and readability. It
provides a vast collection of libraries and modules
that make it an excellent choice for automating
various tasks.
Let’s dive into the world of automation and
discover 9 Python scripts that can simplify your
work and save you time and effort.
ramchandrapadwal
1. Automating File Management
1. 1 — Sorting Files in a Directory
This Python script organizes files in a directory by sorting
them into subdirectories based on their file extensions. It
identifies the file extension and moves the file to the
appropriate subdirectory. This can be useful for decluttering
your downloads folder or organizing files for a specific
project.
ramchandrapadwal
1. 2 — Removing Empty Folders
This Python script searches for and deletes empty folders
within a specified directory. It can help you maintain a clean
and tidy folder structure, especially when dealing with large
sets of data.
1.3 — Renaming Multiple Files
This Python script allows you to rename multiple files in a
directory simultaneously. It takes the old name and the new
name as inputs and replaces the old name with the new one for
all the files that match the specified criteria.
ramchandrapadwal
2. Web Scraping with Python
2. 1 — Extracting Data from a Website
This Python script utilizes the requests and BeautifulSoup
libraries to scrape data from a website. It fetches the content
of the webpage and uses BeautifulSoup to parse the HTML.
You can customize the script to extract specific data like
headlines, product information, or prices.
2.2 — Downloading Images in Bulk
This Python script is designed to download images in bulk
from a website. It assumes that the website provides a JSON
API that returns an array of image URLs. The script then
iterates through the URLs and downloads the images, saving
them to the specified directory.
ramchandrapadwal
2.3 — Automating Form Submissions
This Python script automates form submissions on a website
by sending POST requests with the form data. You can
customize the script by providing the URL and the necessary
form data to be submitted.
ramchandrapadwal
3. Text Processing and Manipulation
3. 1 — Counting Words in a Text File
This Python script reads a text file and counts the number of
words it contains. It can be used to quickly analyze the
content of text documents or to keep track of the word count
in a writing project.
3.2 Finding and Replacing Text
This Python script searches for a specific text in a file and
replaces it with the desired text. It can be helpful for batch-
replacing certain phrases or correcting errors in large text files.
ramchandrapadwal
3.3 Generating Random Text
This Python script generates random text of a specified length.
It can be used for testing and mocking purposes or even as a
source of random content for creative writing.
ramchandrapadwal
4. Automating Emails
4. 1 — Sending Personalized Emails
This Python script enables you to send personalized emails to
a list of recipients. You can customize the sender’s email,
password, subject, body, and the list of recipient emails. Please
note that for security reasons, you should use an application-
specific password when working with Gmail.
ramchandrapadwal
4.2 Emailing File Attachments
This Python script allows you to send emails with file
attachments. Simply provide the sender’s email, password,
recipient’s email, subject, body, and the path to the file you
want to attach.
ramchandrapadwal
4.3 Automatic Email Reminder
This Python script sends automatic email reminders based on
a specified date. It is useful for setting up reminders for
important tasks or events, ensuring you never miss a deadline.
ramchandrapadwal
5. Automating Excel Spreadsheets
5. 1 — Reading & Writing to Excel
This Python script uses the pandas library to read data from
an Excel spreadsheet and write data to a new Excel file. It
allows you to work with Excel files programmatically, making
data manipulation and analysis more efficient.
5.2 Data Analysis and Visualization
This Python script uses pandas and matplotlib libraries to
perform data analysis and visualization. It enables you to
explore datasets, derive insights, and create visual
representations of the data.
ramchandrapadwal
5.3 Merging Multiple Sheets
This Python script merges data from multiple sheets within an
Excel file into a single sheet. It’s handy when you have data
split across different sheets but want to consolidate them for
further analysis.
ramchandrapadwal
6. Interacting with Databases
6. 1 Connecting to a Database
This Python script allows you to connect to an SQLite
database and execute queries. You can adapt it to work with
other database management systems like MySQL or
PostgreSQL by using the appropriate Python database drivers.
6.2 Executing SQL Queries
This Python script is a generic function to execute SQL queries
on a database. You can pass the query as an argument to the
function along with the database connection object, and it will
return the result of the query.
ramchandrapadwal
6.3 Data Backup and Restore
This Python script allows you to create backups of your
database and restore them when needed. It’s a precautionary
measure to protect your valuable data from accidental loss.
ramchandrapadwal
7. Social Media Automation
7. 1 Posting on Twitter and Facebook
This Python script utilizes the Twython and facebook-sdk
libraries to automate posting on Twitter and Facebook. You
can use it to share updates, announcements, or content from
your Python script directly to your social media profiles.
7.2 Automatic Social Media Sharing
This Python script automates sharing random content on
Twitter and Facebook. You can customize it to fetch content
from a list or database and share it periodically on your social
media platforms.
ramchandrapadwal
8. Automating System Tasks
8. 1 Managing System Processes
This Python script uses the psutil library to manage system
processes. It allows you to retrieve the list of running processes
and kill a specific process by its name.
8.2 Scheduling Tasks with Cron
This Python script utilizes the crontab library to schedule
tasks using cron syntax. It enables you to automate the
execution of specific commands at regular intervals or at
specific times.
ramchandrapadwal
8.3 Monitoring Disk Space
This Python script monitors the available disk space on your
system and sends an alert if it falls below a specified threshold.
It’s useful for proactive disk space management and
preventing potential data loss due to insufficient disk space.
ramchandrapadwal
9. Automating Image Editing
9. 1 Image Resizing and Cropping
This Python script uses the Python Imaging Library (PIL) to
resize and crop images. It can be helpful for preparing images
for different display resolutions or specific use cases.
ramchandrapadwal