Automation has become an essential part of our daily lives. A lot of times, we find ourselves doing repetitive tasks that take up much time.
Think about the time spent sending emails, sorting records, or organizing files. These are routine activities that can be predictable, and often, a computer can handle them more efficiently than we can. Instead of manually performing these tedious tasks, we can use automation to have our computer take over.
With the right tools and programming, computers can execute repetitive jobs with speed and accuracy, ensuring that nothing gets missed or done incorrectly. This process not only saves time but also reduces errors that can occur when humans perform the same task over and over again.
Below are some amazing Python automation projects that can ease your stress and improve productivity.
Managing Files and Folders
This project helps you organize files by sorting them into folders based on their file types. It creates separate folders for each file type (such as “PDFs” or “Images”) and moves the corresponding files into their designated folders.
import os import shutil source_folder = "source" destination_folder = "destination" for file in os.listdir(source_folder): if os.path.isfile(os.path.join(source_folder, file)): ext = file.split('.')[-1] ext_folder = os.path.join(destination_folder, ext) os.makedirs(ext_folder, exist_ok=True) shutil.move(os.path.join(source_folder, file), os.path.join(ext_folder, file)) print("Successful!")
Bulk renaming of files
The script below enables you to rename multiple files simultaneously within a specified folder. It follows a predefined naming pattern for the new file names to ensure consistency and organization. This is particularly useful for renaming photos, documents, or any set of files that require uniform naming.
You can customize the pattern to include sequential numbering, timestamps, or specific keywords, making it easier to manage large batches of files efficiently.
import os folder_path = "source_copy" prefix = "file_" files = [file for file in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, file))] i = 1 for file in files: file_extension = file.split('.')[-1] new_name = f"{prefix}{i}.{file_extension}" old_path = os.path.join(folder_path, file) new_path = os.path.join(folder_path, new_name) os.rename(old_path, new_path) i += 1 print(f"Files have been renamed '{prefix}'!")
Scraping and Collecting Data from the Web
Web scrapping involves extracting valuable information from websites, like prices, reviews, or news articles. This example demonstrates how to scrape a webpage and extract all the hyperlinks (URLs) present in it. Using the requests library, the script sends a GET request to the specified URL. Then, it uses BeautifulSoup to parse the HTML content. The script locates all <a> tags with href attributes and collects the links into a list. Finally, it prints out the list of collected links.
import requests from bs4 import BeautifulSoup url = "https://www.wikipedia.org/" response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') # Extract all the links from the webpage links = [a['href'] for a in soup.find_all('a', href=True)] for link in links: print(link)
Automating Reports with Excel
This script demonstrates how to automate the creation of a basic Excel report. It starts by defining sample data as a dictionary with names and scores. The dictionary is then converted into a pandas DataFrame, which allows for easy manipulation of data.
The script proceeds by saving the DataFrame as an Excel file using to_excel(), and prints a confirmation message with the file path. This is a quick way to generate and store reports without manual input, especially useful for data analysis or reporting tasks.
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Score': [85, 90, 95]} df = pd.DataFrame(data) file_path = "report.xlsx" df.to_excel(file_path, index=False) print(f"Report saved to {file_path}")
Managing and Cleaning Data
Data cleaning and preparation could take a lot of time, but you can automate this using Python, saving hours of manual work.
This Python script demonstrates how to automate the process of handling missing data in a dataset using pandas. It starts by loading data with missing values (None) into a DataFrame. The script then cleans the data by replacing missing values: it fills the “Name” column with ‘Unknown’ and the “Score” column with the mean of the available scores. The changes are applied directly to the original DataFrame with inplace=True. Finally, the script prints the cleaned DataFrame, showing the data after the missing values have been filled in.
import pandas as pd input_file = "data.csv" df = pd.read_csv(input_file) df.loc[:, 'Name'] = df['Name'].fillna('Unknown') df.loc[:, 'Score'] = df['Score'].fillna(df['Score'].mean()) output_file = "cleaned_data.csv" df.to_csv(output_file, index=False) print(f"Cleaned data saved to {output_file}")
Conclusion
Automation is no longer a luxury; it’s a necessity in today’s fast-paced world. You can automate almost anything, and the hours you free up become time for the work that matters. With Python, you can automate a wide range of tasks, such as sending emails, organizing files, managing data, and even controlling smart home devices. Whether for personal use or business efficiency, automation helps streamline workflows and boost productivity.