In this article, we are going to see how to schedule a Python Script to run daily. Scheduling a Python Script to run daily basically means that your Python Script should be executed automatically daily at a time you specify.
Preparing the Python Script.
Create a Python Script that you want to schedule. In our example, we made a simple Python Script that will Open our Mail account to check our emails daily. You’ll need to import the ‘webbrowser’ module.
Alternatively, you may use any Python script that you’d like to schedule.
Python3
# Modules which need to be imported import webbrowser # Site which you want to Open in your # Browser url = "mail.google.com" # Below code is used to specify # location of webbrowser which you # want to use. chrome_path = r'C: \Program Files(x86)\Google\ / Chrome\Application\chrome.exe' # Below code will open your URL webbrowser.register( 'chrome' , None , webbrowser.BackgroundBrowser(chrome_path)) webbrowser.get( 'chrome' ).open_new_tab(url) |
Note: Change your chrome_path according to the location on your system if you want to use the above Python Script.
Now there are two ways to schedule a script:
- Using batch files.
- Using Windows Task Scheduler.
Method 1: Using batch file
Step 1: Create a Batch File.
Open Notepad and follow this generic structure :
“Path where your Python exe is stored\python.exe” “Path where your Python script is stored\script name.py”
pause
Example of the batch file:
Finally, save the Notepad with your file name and the “.bat” extension anywhere on your machine, Eg – ‘automation.bat’.
Method 2: Using Windows Task Scheduler.
Step 1: Open Task Scheduler Application on your Windows Machine.
Location -> C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools\Task Scheduler
Step 2: Click on ‘Create Basic Task….’ in the Actions Tab. And give a suitable Name and Description of your task that you want to Automate and click on Next.
Step 3: In the next step, you have to select at what time intervals your script should be executed. Select ‘Daily’ and click Next. Now you need to specify at what time your Python Script should be executed daily and then Click on Next.
Step 4: In the next step, you need to select the ‘Start a Program’ option from the given options and then click Next. And there choose (Optional – for users who created Batch File) Now if you followed ‘Batch file step’, you just need to enter the file location of the Batch file you created followed by ‘\yourbatchfilename.bat’ in Program/Scripts field and click on Next and Finish your Task, your python Script will now run daily at your specified time.
In this section, you’ll need the file location of the following files:
- python.exe – The path where python.exe is stored (you also need to add ‘\python.exe’ at the end of the location.)
- the path where your python script file is stored.
In our case :
Path of python.exe – C:\Python39\python.exe
Path of my python script – D:\Tutorials\Python
Step 5: Add respective file locations as shown in the figure below and arguments input, specify your python script name.
Step 6: In the next tab, you just need to verify your Inputs and then click on ‘Finish’.
That’s it, your Python Script is now Scheduled and will be executed daily at your Specified time. Here is the Screenshot of our Python Script that got executed.