0% found this document useful (0 votes)
5 views

automation scripts

The document provides a collection of useful Python automation scripts for various daily tasks, including bulk file renaming, automatic email sending, web scraping for news, converting text to speech, downloading YouTube videos, organizing files by type, checking internet speed, monitoring website status, scheduling PC shutdown, and fetching weather updates. Each script includes sample code and brief descriptions of its functionality. Additionally, tips for using certain features, like enabling less secure apps for Gmail and obtaining an API key for weather updates, are included.

Uploaded by

mini10
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

automation scripts

The document provides a collection of useful Python automation scripts for various daily tasks, including bulk file renaming, automatic email sending, web scraping for news, converting text to speech, downloading YouTube videos, organizing files by type, checking internet speed, monitoring website status, scheduling PC shutdown, and fetching weather updates. Each script includes sample code and brief descriptions of its functionality. Additionally, tips for using certain features, like enabling less secure apps for Gmail and obtaining an API key for weather updates, are included.

Uploaded by

mini10
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Here are some useful Python automation scripts that can make daily tasks easier:

1. Bulk File Renamer

Renames all files in a directory based on a given pattern.

import os

folder_path = "/path/to/directory"

prefix = "new_name_"

for count, filename in enumerate(os.listdir(folder_path)):

old_path = os.path.join(folder_path, filename)

new_path = os.path.join(folder_path, f"{prefix}{count}.txt")

os.rename(old_path, new_path)

print("Files renamed successfully!")

2. Automatic Email Sender

Sends emails automatically using smtplib.

import smtplib

from email.message import EmailMessage

def send_email():

email = "[email protected]"

password = "your_password"

recipient = "[email protected]"

msg = EmailMessage()

msg["Subject"] = "Automated Email"

msg["From"] = email

msg["To"] = recipient

msg.set_content("Hello! This is an automated email.")

with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:


server.login(email, password)

server.send_message(msg)

send_email()

print("Email sent successfully!")

🔹 Tip: Enable Less Secure Apps or use an App Password for Gmail.

3. Web Scraping for Latest News

Fetches the latest headlines from a website.

import requests

from bs4 import BeautifulSoup

url = "https://news.ycombinator.com/"

response = requests.get(url)

soup = BeautifulSoup(response.text, "html.parser")

for idx, item in enumerate(soup.find_all("a", class_="storylink")[:5], start=1):

print(f"{idx}. {item.text}")

4. Convert Text to Speech

Converts text into speech using pyttsx3.

import pyttsx3

engine = pyttsx3.init()

engine.say("Hello! This is an automated voice.")

engine.runAndWait()

5. Download YouTube Videos

Downloads videos using pytube.

from pytube import YouTube

url = "https://www.youtube.com/watch?v=your_video_id"

yt = YouTube(url)
yt.streams.get_highest_resolution().download()

print("Download Complete!")

6. Automatically Organize Files by Type

Moves files into folders based on their extensions.

import os

import shutil

folder_path = "/path/to/your/downloads"

file_types = {

"Images": [".jpg", ".jpeg", ".png", ".gif"],

"Documents": [".pdf", ".docx", ".txt"],

"Videos": [".mp4", ".mkv"],

"Music": [".mp3", ".wav"]

for file in os.listdir(folder_path):

file_path = os.path.join(folder_path, file)

if os.path.isfile(file_path):

for folder, extensions in file_types.items():

if file.endswith(tuple(extensions)):

new_folder = os.path.join(folder_path, folder)

os.makedirs(new_folder, exist_ok=True)

shutil.move(file_path, new_folder)

print("Files organized successfully!")

7. Check Internet Speed

Measures internet speed using speedtest-cli.

import speedtest

st = speedtest.Speedtest()
download_speed = st.download() / 1_000_000

upload_speed = st.upload() / 1_000_000

print(f"Download Speed: {download_speed:.2f} Mbps")

print(f"Upload Speed: {upload_speed:.2f} Mbps")

8. Monitor Website Status

Checks if a website is up or down.

import requests

url = "https://example.com"

try:

response = requests.get(url)

if response.status_code == 200:

print(f"{url} is UP!")

else:

print(f"{url} is DOWN!")

except requests.ConnectionError:

print(f"{url} is NOT REACHABLE!")

9. Auto Shutdown PC

Schedules a shutdown in 60 seconds.

import os

os.system("shutdown /s /t 60") # Windows

# os.system("shutdown -h +1") # Linux/Mac (1-minute delay)

10. Get Weather Updates

Fetches the current weather for any city using OpenWeatherMap API.

import requests

API_KEY = "your_api_key"
city = "New York"

url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"

response = requests.get(url).json()

print(f"Weather in {city}: {response['weather'][0]['description']}, {response['main']['temp']}°C")

🔹 Tip: Get an API key from OpenWeather.

Would you like scripts for anything specific? 😊

You might also like