0% found this document useful (0 votes)
5 views4 pages

Python Automation Program

This document outlines a Python script for automating file operations and sending notification emails. It includes configuration settings for email and directories, functions for moving files, sending emails, and scheduling daily tasks. The script is designed to run daily at a specified time, logging the operations performed and any errors encountered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python Automation Program

This document outlines a Python script for automating file operations and sending notification emails. It includes configuration settings for email and directories, functions for moving files, sending emails, and scheduling daily tasks. The script is designed to run daily at a specified time, logging the operations performed and any errors encountered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import os

import shutil

import smtplib

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

import schedule

import time

# --- Configuration ---

SENDER_EMAIL = "your_email@example.com" # Replace with your email

SENDER_PASSWORD = "your_app_password" # Replace with your app


password (NOT your regular password)

RECEIVER_EMAIL = "recipient_email@example.com" # Replace with


recipient email

SMTP_SERVER = "smtp.gmail.com" # For Gmail, adjust for other providers

SMTP_PORT = 587 # For TLS, adjust for other providers

SOURCE_DIR = "C:\\Users\\YourUser\\Documents\\AutomationSource" #
Replace with your source directory

DEST_DIR = "C:\\Users\\YourUser\\Documents\\AutomationDestination" #
Replace with your destination directory

LOG_FILE = "automation_log.txt"

# --- Function 1: File Operations ---

def perform_file_operations():

print(f"[{time.ctime()}] Starting file operations...")

if not os.path.exists(DEST_DIR):

os.makedirs(DEST_DIR)

print(f"[{time.ctime()}] Created destination directory: {DEST_DIR}")


files_moved = 0

for filename in os.listdir(SOURCE_DIR):

source_path = os.path.join(SOURCE_DIR, filename)

dest_path = os.path.join(DEST_DIR, filename)

if os.path.isfile(source_path):

try:

shutil.move(source_path, dest_path)

print(f"[{time.ctime()}] Moved: {filename} to {DEST_DIR}")

files_moved += 1

except Exception as e:

print(f"[{time.ctime()}] Error moving {filename}: {e}")

with open(LOG_FILE, "a") as f:

f.write(f"[{time.ctime()}] File operations completed. Moved


{files_moved} files.\n")

print(f"[{time.ctime()}] File operations completed. Moved


{files_moved} files.")

# --- Function 2: Send Email ---

def send_notification_email(subject, body):

print(f"[{time.ctime()}] Attempting to send email...")

msg = MIMEMultipart()

msg['From'] = SENDER_EMAIL

msg['To'] = RECEIVER_EMAIL

msg['Subject'] = subject

msg.attach(MIMEText(body, 'plain'))

try:

with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:


server.starttls() # Secure the connection

server.login(SENDER_EMAIL, SENDER_PASSWORD)

server.send_message(msg)

print(f"[{time.ctime()}] Email sent successfully to


{RECEIVER_EMAIL}")

with open(LOG_FILE, "a") as f:

f.write(f"[{time.ctime()}] Email sent: '{subject}'\n")

except Exception as e:

print(f"[{time.ctime()}] Failed to send email: {e}")

with open(LOG_FILE, "a") as f:

f.write(f"[{time.ctime()}] Failed to send email: {e}\n")

# --- Function 3: Main Automation Task ---

def daily_automation_task():

print(f"\n[{time.ctime()}] Running daily automation task...")

# 1. Perform file operations

perform_file_operations()

# 2. Send a summary email

subject = "Daily Automation Report"

body = f"""

Dear User,

This is your daily automation report.

File operations completed. Please check the '{DEST_DIR}' directory for


moved files.

Log file: {LOG_FILE}


Best regards,

Your Automation Script

"""

send_notification_email(subject, body)

print(f"[{time.ctime()}] Daily automation task completed.")

# --- Scheduling Tasks ---

def setup_schedule():

print(f"[{time.ctime()}] Setting up daily schedule...")

# Schedule the daily_automation_task to run every day at 10:00 AM

schedule.every().day.at("10:00").do(daily_automation_task)

# You can add other schedules here, e.g.:

# schedule.every(10).minutes.do(some_other_task)

# schedule.every().hour.do(yet_another_task)

print(f"[{time.ctime()}] Schedule set. Waiting for tasks to run...")

# --- Main Program Execution ---

if __name__ == "__

You might also like