Python Automation Scripts

Python 187 views Nov 05, 2025 2 min read

Python Automation Scripts

Python is widely used for automation tasks, allowing developers to automate repetitive tasks, file operations, web scraping, emails, and system administration.


1. File and Folder Automation

  • Creating, renaming, moving, and deleting files/folders using os and shutil.

import os import shutil # Create folder os.mkdir('TestFolder') # Rename folder os.rename('TestFolder', 'NewFolder') # Move folder shutil.move('NewFolder', 'DestinationPath') # Delete folder os.rmdir('DestinationPath/NewFolder')

2. Working with CSV and Excel Files

  • Reading and writing data using pandas.

import pandas as pd # Read CSV df = pd.read_csv('data.csv') # Write CSV df.to_csv('output.csv', index=False) # Read Excel df_excel = pd.read_excel('data.xlsx') # Write Excel df_excel.to_excel('output.xlsx', index=False)

3. Web Automation

  • Automate browsers using Selenium.

from selenium import webdriver driver = webdriver.Chrome(executable_path='chromedriver') driver.get("https://www.google.com") search_box = driver.find_element('name', 'q') search_box.send_keys("Python Automation") search_box.submit()

4. Email Automation

  • Send emails using smtplib.

import smtplib server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login('youremail@gmail.com', 'password') server.sendmail('youremail@gmail.com', 'recipient@gmail.com', 'Hello, this is an automated email!') server.quit()

5. Web Scraping

  • Extract data from websites using BeautifulSoup.

import requests from bs4 import BeautifulSoup url = "https://example.com" response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # Extract all links links = [a['href'] for a in soup.find_all('a', href=True)] print(links)

6. Scheduling Tasks

  • Run scripts automatically at intervals using schedule or cron jobs.

import schedule import time def job(): print("Running automated task...") schedule.every(10).seconds.do(job) while True: schedule.run_pending() time.sleep(1)

7. Key Points

  • Python automates file handling, web tasks, emails, and reports.

  • Libraries like os, shutil, pandas, selenium, requests, BeautifulSoup are commonly used.

  • Can schedule tasks using schedule module or operating system cron jobs.

  • Saves time, reduces human errors, and improves productivity.

Some advanced sections are available for Registered Members
Share this Post
🚀 Want to Test Your Knowledge?

Take quizzes related to this topic and see where you stand!

Start Quiz Now
Back to Tutorials