Schedule a Python Script on Task Scheduler in Windows 10

Schedule a Python Script on Task Scheduler in Windows 10

This article explains the process of scheduling a Python script in Windows 10. Task Scheduler in Windows 10 is a program or a tool through which you can schedule a task to be executed.

You can choose some predefined actions to be executed when a certain condition is fulfilled.

For example, you can run a backup script every night at a fixed or scheduled time. You can make a task scheduler to send an email to your account when a predefined event occurs.

Schedule a Python Script in Windows 10

You need multiple steps to schedule a Python script with Windows Task Scheduler.

  1. First, check if the Python interpreter is installed on Windows or not. To check this, open the command prompt and type python3, as shown in the below picture.

    You will get the output like this. It means the Python interpreter is already installed.

    cmd python3

    The Python command line will also be opened. To exit this, press Ctrl+Z and then enter.

  2. Next, you need to find out where the Python interpreter is installed. To check this, write the following command.

    python -c "import os, sys; print(os.path.dirname(sys.executable))"
    

    This will return the path like this:

    os path dirname

You can verify this by going to the output directory. Save this path with `python.exe` for later use.
  1. You need to prepare the script you want to schedule. For demo purposes, we will write a simple script that writes the time to a text file every time it runs.

    The code is as follows:

    import datetime
    text_file = open(r'C:\Users\senar\Desktop\Content Writing\schedule script\result.txt', 'a')
    text_file.write(f'{datetime.datetime.now()} - the script ran. \n')
    

    The above code opens a text file and writes its current time. Save this script as a .py file in some directory and note down its path.

    Now we have two file paths—one for the script and one for the Python interpreter from step 2.

  2. Go to Windows startup, search Task Scheduler, and open it up. The following window will appear.

    Task Scheduler

  3. Select Create Task from the right sidebar and enter the task details, as shown below.

    Create Task

  4. Go to the Triggers tab and press the New button. The below window will appear.

    This is where you will enter scheduling details. Enter the details and press the OK button.

![New Trigger](/img/Python/New Trigger.png)
  1. Go to the Actions tab and press the New button.

    Actions tab

  2. In the Actions tab, choose Start a program as Action and enter the following three details.

    • Program/script - the path to Python interpreter from step 2.
    • Add arguments (optional) - script name (e.g. schedule_script.py)
    • Start in (optional) - the path to Python script from step 3.

New Action

After that, press OK and then OK again. The following window will appear.

Enter the password and press OK. Your task will appear in the scheduled tasks.

Scheduled task

This is where you have completed the scheduling process. You have activated the trigger, and your Python script code will run automatically per your added trigger details.

The above script will produce the results.

task log

After every 5 minutes, the Python script will run automatically and append the details on the next line, as shown above.