Automation Scripts Part1
Automation Scripts Part1
import os
extension = os.path.splitext(filename)[1]
new_filename = f"{new_name}_{count+1}{extension}"
os.rename(old_path, new_path)
directory = "/path/to/your/directory"
new_name = "new_filename"
batch_rename_files(directory, new_name)
2. Backup Files and Folders
import shutil
import os
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
backup_destination = f"{destination}_backup_{timestamp}"
shutil.copytree(source, backup_destination)
source = "/path/to/your/source"
destination = "/path/to/your/destination"
backup_files_and_folders(source, destination)
3. Move Files Based on Extension
import os
import shutil
if not os.path.exists(target_directory):
os.makedirs(target_directory)
if filename.endswith(extension):
source_directory = "/path/to/source"
target_directory = "/path/to/target"
extension = ".txt"
import os
import shutil
def organize_files_by_date(directory):
if os.path.isfile(file_path):
modification_time = os.path.getmtime(file_path)
date_folder = datetime.fromtimestamp(modification_time).strftime('%Y-%m-%d')
if not os.path.exists(date_folder_path):
os.makedirs(date_folder_path)
shutil.move(file_path, date_folder_path)
directory = "/path/to/your/directory"
organize_files_by_date(directory)
5. Monitor Directory Changes
import time
class MonitorHandler(FileSystemEventHandler):
print(f"Modified: {event.src_path}")
print(f"Created: {event.src_path}")
print(f"Deleted: {event.src_path}")
directory_to_monitor = "/path/to/your/directory"
event_handler = MonitorHandler()
observer = Observer()
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
6. Send Automatic Emails with Attachments
import smtplib
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
part.set_payload((attachment).read())
encoders.encode_base64(part)
{os.path.basename(attachment_path)}")
msg.attach(part)
server = smtplib.SMTP(smtp_server, port)
server.starttls()
server.login(login, password)
text = msg.as_string()
server.quit()
smtp_server = "smtp.yourserver.com"
port = 587
sender_email = "[email protected]"
receiver_email = "[email protected]"
subject = "Subject"
attachment_path = "/path/to/your/attachment"
login = "[email protected]"
password = "your_password"
import requests
import time
r = requests.get(url)
f.write(r.content)
url = "http://example.com/file.zip"
save_path = "/path/to/your/save/file.zip"
while True:
download_file(url, save_path)
time.sleep(interval)
8. Convert CSV Files to Excel
import pandas as pd
df = pd.read_csv(csv_file)
df.to_excel(excel_file, index=False)
csv_file = "/path/to/your/file.csv"
excel_file = "/path/to/your/file.xlsx"
convert_csv_to_excel(csv_file, excel_file)
9. Combine Multiple CSV Files into One
import pandas as pd
import os
f.endswith('.csv')]
combined_df.to_csv(output_file, index=False)
directory = "/path/to/your/csv/files"
output_file = "/path/to/your/output.csv"
combine_csv_files(directory, output_file)
10. Generate PDF Reports from Data
import pandas as pd
class PDF(FPDF):
def header(self):
def footer(self):
self.set_y(-15)
self.set_font('Arial', 'I', 8)
self.ln(10)
self.ln()
pdf = PDF()
pdf.add_page()
pdf.chapter_title('Data Report')
pdf.chapter_body(data.to_string())
pdf.output(output_pdf)
data = pd.DataFrame({
})
output_pdf = "/path/to/your/report.pdf"
generate_pdf_report(data, output_pdf)