Step 1
First let us identify the where python.exe is situated. Open the command prompt and type in "where python".
Step 2
Next we need to give the path where the python script that needs to be executed is present. In my case it is C:\Users\pavan\OneDrive\Documents\testpgm.py
Open the Windows Task Scheduler. Click on Create Task. It will open up a new dialog box. Fill in a name to the task and give a description. Choose Run only when user is logged on
Step 3
Click on "Action" tab and fill the program/script as C:\ProgramData\Anaconda3\python.exe
For Action retain the default Start a program. For Add arguments(optional), we need to fill in the script testpgm.py. And for the Start in (optional), let us fill in the path to the script C:\Users\pavan\OneDrive\Documents
Step 4
Next click on the "Triggers" tab and choose as required.
Step 5
One point to note. Every time we run the task in the scheduler, it opens up the console window as shown below.
Step 6
Opening a console window is fine in a test environment. But in a production environment we could have multiple programs scheduled simultaneously and this could pose a problem. One solution to this problem is to change the python executable from python.exe to pythonw.exe
Running a sequence of python scripts under one main python scrip
testpgm.py is the main program run by the windows task scheduler. Now let us say that we want to run three python scripts sequentially within the main program. Lets call them as pgm1.py, pgm2.py, pgm3.py. This is how we would do it:
import os # Set the file and directory paths full_file_path = os.path.realpath(__file__) # the actual file path (during runtime only) current_directory = os.path.dirname(full_file_path) # the directory path of the current file one_folder_back = os.path.normpath(os.getcwd() + os.sep + os.pardir) pgm1_file_path = current_directory + "\\pgm1.py" os.system('python ' + pgm1_file_path) print("Finished execution of pgm1") pgm2_file_path = current_directory + "\\pgm2.py" os.system('python ' + pgm2_file_path) print("Finished execution of pgm2") pgm3_file_path = current_directory + "\\pgm3.py" os.system('python ' + pgm3_file_path) print("Finished execution of pgm3")
No comments:
Post a Comment