In our previous Python tutorial, we have explained how to make ChatBot Project with Python and Flask. In this tutorial, we will explain how to Automate Everything with Python.
Automation is the key of efficiency and productivity After a decade of experiance. It lets us to automate important and repeated tasks to complete quickly.
Python is an incredibly powerful and flexible language. It can be used to automate things by building utilities of useful scripts to make life easier as a software developer.
So, let’s proceed with to explore these useful scripts that can speedup the way we work.
1. Make ChatBot with ChatGPT
You can make your own ChatBot using OpenAi to make your life easier and faster.
import openai openai.api_key = "API_KEY" promptText = input("\nYou: \n") modelEngine = "text-davinci-003" completion = openai.Completion.create( engine=modelEngine, prompt=promptText, max_tokens=1024, n=1, stop=None, temperature=0.5, ) response = completion.choices[0].text print("\nBot Say: ", response)
2. Email Responder
It’s always very time consuming to respond to each and every email. You can create an auto responder script for handling emails reply. The script will automatically send pre-defined responses to emails.
from smtplib import SMTP from ssl import create_default_context as context from email.message import EmailMessage def autoResponder(sender, recipient, subject, message): msg = EmailMessage() msg.set_content(message) msg["Subject"] = subject msg["From"] = sender msg["To"] = recipient with SMTP('smtp.domain.example', 587) as server: server.starttls(context=context()) server.login('email@domain.example', 'your-password') server.send_message(msg)
3. Make File Organizer
Python can also help you to organize files and directories in your computer by using Python’s OS and shutil modules. You can create a script to manage files on your computer Desktop to renaming files, creating folders, etc.
import os, shutil dirList=[] i=1 destDir='C:/Users/Desktop/Everything' while os.path.exists(destDir): destDir+=str(i) i+=1 os.makedirs(destDir) dirList = os.listdir('/Users/NAME/Desktop') for dir in dirList: print (dir) if dir ==__file__: continue shutil.move(dir, destDir)
4. Make Web Scraper
If you frequently need websites data, then you can make a web scraper using BeautifulSoup module to get webiste data for further use.
import requests from bs4 import BeautifulSoup url = 'https://example.com' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') data = soup.find('div', {'class': 'content'}).text print(data)
5. Backup Everything
You can create a Python script to backup everything after work completed.
import shutil from datetime import date import os import sys os.chdir(sys.path[0]) def takeBackup(srcFileName, dstFileName=None, srcDir='', dstDir=''): try: today = date.today() date_format = today.strftime("%d_%b_%Y_") srcDir = srcDir+srcFileName if not srcFileName: print("Please provide atleast the source file name") exit() try: if srcFileName and dstFileName and srcDir and dstDir: srcDir = srcDir+srcFileName dstDir = dstDir+dstFileName elif dstFileName is None or not dstFileName: dstFileName = srcFileName dstDir = dstDir+date_format+dstFileName elif dstFileName.isspace(): dstFileName = srcFileName dstDir = dstDir+date_format+dstFileName else: dstDir = dstDir+date_format+dstFileName shutil.copy2(srcDir, dstDir) print("Backup Successful!") except FileNotFoundError: print("File does not exists!,\ please provide the complete path") except PermissionError: dstDir = dstDir+date_format+dstFileName shutil.copytree(srcFileName, dstDir) takeBackup("data.txt")
6. Schedule Computer Shutdown
If you’re done with your work on specific time then you can create computer shutdown sceduler to shutdown your computer on that specific time everyday.
import schedule import time import os when = "20:00" print("The computer will be shutdown at " + when + "") def shudownComputer(): os.system("shutdown /s /t 1") schedule.every().day.at(when).do(shudownComputer) while True: schedule.run_pending() time.sleep(1)
Conclusion
By utilizing the power of Python automation, you can reduce error, save time and enhance productivity.