5 python automation scripts that will make your life easier

Techtonic Tales
5 min readMar 31, 2023

--

Hi everyone! I’m back with an exciting blog post that will revolutionise the way you work. In today’s digital age, automation is key to productivity, and Python is the ultimate tool for automating repetitive tasks. Whether you’re a developer, data scientist, marketer, or just someone who wants to simplify their life, Python automation scripts can help you achieve your goals faster and with less effort. In this post, we’ll share with you five of our favourite Python automation scripts that will help you save time, make your work easier, and increase you productivity. So get ready to level up your game and join the world of Python automation!

So let’s get started!

1.Automated File Organizer

This script can automatically organize files in a specific directory based on their file type, extension, or any other criteria. For example, you can create a script that moves all image files to a designated folder, all text files to another folder, and so on.

import os
import shutil

# set the directory you want to organize
directory = '/path/to/directory'

# create folders for file types you want to organize
image_folder = os.path.join(directory, 'Images')
if not os.path.exists(image_folder):
os.mkdir(image_folder)

text_folder = os.path.join(directory, 'Text')
if not os.path.exists(text_folder):
os.mkdir(text_folder)

# move files to corresponding folders based on file type
for filename in os.listdir(directory):
if filename.endswith('.jpg') or filename.endswith('.png'):
shutil.move(os.path.join(directory, filename), image_folder)
elif filename.endswith('.txt') or filename.endswith('.docx'):
shutil.move(os.path.join(directory, filename), text_folder)
this is the example on my own pc

2. Social Media Post Scheduler

This script allows you to schedule your social media posts in advance. You can use it to post updates to multiple social media platforms simultaneously, at the times you choose.

import schedule
import time
import facebook
import tweepy
from instapy import InstaPy
# Set your Facebook access token and page ID
ACCESS_TOKEN = 'your_access_token_here'
PAGE_ID = 'your_page_id_here'

# Set your Twitter API credentials
CONSUMER_KEY = 'your_consumer_key_here'
CONSUMER_SECRET = 'your_consumer_secret_here'
ACCESS_TOKEN_KEY = 'your_access_token_key_here'
ACCESS_TOKEN_SECRET = 'your_access_token_secret_here'

# Set your Instagram credentials
USERNAME = 'your_instagram_username_here'
PASSWORD = 'your_instagram_password_here'
# Sample function that posts updates on Facebook, Twitter, and Instagram
def post_updates():
# Code to post updates on Facebook
graph = facebook.GraphAPI(access_token=ACCESS_TOKEN, version="3.0")
message = "Hello from my Python script!"
graph.put_object(parent_object=PAGE_ID, connection_name='feed', message=message)
print("Posted update on Facebook.")

# Code to post updates on Twitter
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
tweet = "This is a test tweet."
api.update_status(tweet)
print("Posted update on Twitter.")

# Code to post updates on Instagram
session = InstaPy(username=USERNAME, password=PASSWORD)
session.login()
session.upload_photo("path/to/photo.jpg", "Check out my new photo!")
session.end()
print("Posted update on Instagram.")
# Schedule the function to run at a specific time
schedule.every().day.at("12:00").do(post_updates)

# Run the scheduler continuously
while True:
schedule.run_pending()
time.sleep(1)

3. Automatic Backup Script

This script can automatically backup your important files, documents, and folders on a regular basis. It can be configured to backup to an external drive, cloud storage, or any other location of your choice.

import shutil
import datetime

# define the directories you want to backup
directories = ['/path/to/important/folder1', '/path/to/important/folder2']

# set the backup directory
backup_directory = '/path/to/backup/folder'

# create a timestamp for the backup folder name
timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
backup_folder = os.path.join(backup_directory, timestamp)

# create the backup folder
os.mkdir(backup_folder)

# copy the directories to the backup folder
for directory in directories:
shutil.copytree(directory, os.path.join(backup_folder, os.path.basename(directory)))

4. Email Automation

This is the most important script that I use on daily bases may it be my day job or my side jobs. This script can be used to automate email sending and receiving. For example, you can create a script that will send an email to all subscribers of your website or blog whenever a new post is published.

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

# Email and password for the sender's account
sender_email = 'sandermail'
sender_password = 'apppassword'

# Email addresses for the sender and receiver
from_addr = 'sandermail'
to_addr = 'receivermail'

# Create a multipart message with subject, body, and attachment (optional)
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = 'Test Email'
body = 'This is a test email sent from Python.'
msg.attach(MIMEText(body, 'plain'))

# Add an attachment (optional)
filename = 'example.txt'
with open(filename, 'rb') as attachment:
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename= {filename}')
msg.attach(part)

# Connect to the SMTP server and send the email
smtp_server = 'smtp.gmail.com'
smtp_port = 587
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
text = msg.as_string()
server.sendmail(from_addr, to_addr, text)

in here we are using google’s smtp server google doesn’t allow you gmail password being used as their security measures so we have to create a separate app password for this:

for this you just have to goto https://myaccount.google.com/apppasswords and do the following:

this will give you a password that you can add in sender_password variable

5. Web Scraping Script

This script can be used to scrape data from websites and store it in a database or spreadsheet. It can be useful for collecting information for research, analysis, or marketing purposes.

this particular script is used to scrape links from a articular website

import requests
from bs4 import BeautifulSoup

# Define the URL to scrape
url = 'https://stackoverflow.com/questions/16512592/login-credentials-not-working-with-gmail-smtp'

# Send a GET request to the URL
response = requests.get(url)

# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')

# Find all the <a> tags on the page
links = soup.find_all('a')

# Print out the text and href attribute for each link
for link in links:
print(link.text.strip(), link.get('href'))

Alright guys, that’s all for today’s share. I’ll share more python and automation stuff in the future and also how to set these up in a cron job using VPS and will share more other interesting stuff. If you liked it, give it a like.

--

--

Techtonic Tales
Techtonic Tales

No responses yet