10

Schedule Your Python Scripts On Windows Platform

 3 years ago
source link: https://towardsdatascience.com/schedule-your-python-scripts-on-windows-platform-4c0b756b81e9
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Schedule Your Python Scripts On Windows Platform

Productivity At Your Fingertips

Image for post
Image for post
Photo by Fabian Albert on Unsplash

Let’s think of a scenario before we start with this tutorial:

Your manager calls you and instructs, “the client wants refreshed data every day by 6 AM IST. Please change your shift timings and ensure script execution post client’s batch completes”. What does this mean? This means, starting today, you will start losing your precious sleep, time with family and friends, and above all, will be working on executing some random script at the most unproductive hour of the day.

If the above sounds familiar and you want to save yourself from a nightmare like this, please read further. In this tutorial, we will learn to schedule Python scripts on a Windows platform.

First Things First

The achieve our objective, we need 3 things:

  • Python Script —For this tutorial, I have written a small Python code that reads a “CSV” file from my Windows folder location. This “CSV” file contains 2 columns, each having random numbers. The code adds both the columns to create a new one and saves this new version of the CSV file at the same folder location.
#### Importing the required library
import pandas as pd#### Reading csv file
df = pd.read_csv("C:\\Ujjwal\\New_File_V1.csv")#### Adding 2 columns
df["Third_Column"] = df["Randome Numbers 1"] + df["Random Numbers 2"]#### Exporting the data to same location
df.to_csv("C:\\Ujjwal\\New_File_V2.csv",index = False)
  • Batch File — A batch file is a script file with a “.BAT” extension. These files are generally saved as simple text files and contain commands which can be executed on the command-line interface (command prompt). When the command prompt executes these files, it reads through the commands written in the file and executes them line by line.
  • Scheduler — Last but not the least, we need a scheduler that can read the Batch file and executes the commands written in it at a set time. For this purpose, we will use Windows’ Task Scheduler will come in handy.

Preparing Batch File

In the previous section, we have already shared the sample Python code which we plan to schedule. The second component required is a Batch file. For our purpose, the Batch file will have 2 commands:

  • Location of the Python Application— This is the Python application (“.exe” extension) which is used for the execution of the scripts. In our batch file, we will provide the location of this application as the first command. On my system, this location is as below:
#### Location of Python Executable
C:\Users\Ujjwal\Anaconda3\python.exe
  • Location of the Python Script — This is the script that you want to schedule. We will provide the location of this script as the second command to the Batch file. Given we are working with Python scripts, ensure that the backslashes in the folder location are replaced with forward slash. The Windows location of my script is as follows.
#### Windows Location
C:\Ujjwal\Executable.py#### Backslash replaced with forward slash
C:/Ujjwal/Executable.py
  • Final Script — The final Batch script will look something like below. Save this script in a text file with a .BAT extension at any Windows location.
#### Final Batch Script
C:\Users\Ujjwal\Anaconda3\python.exe "C:/Ujjwal/Executable.py"

Setting up the schedule

In this step, we will set up the schedule to execute our tasks periodically. The step by step process is as follows:

  • Open Windows Task Scheduler — Go to Windows search, search for task scheduler, and open it. The task scheduler interface will look something like below:
Image for post
Image for post
Task Scheduler Interface (Image by Author)
  • Create the New Task — Click on “Create Basic Task” in the right window pane to create a new task. A new window, prompting you to fill the task name and description will pop-up. The interface will look like below:
Image for post
Image for post
New Window to Create Basic Task (Image by Author)
  • Select Periodicity — Click next after filling the name and description details of the task. Now, the system will prompt you to select the trigger options where we can set the periodicity of the task. For our purpose, we are selecting the daily execution. A complete list of trigger options is as follows:
Image for post
Image for post
Schedule Periodicity (Image by Author)
  • Select Schedule Time — Click next after selecting the task frequency. Now, the system will prompt you to select a specific time at which you want the schedule to trigger. The interface screen would look something like below:
Image for post
Image for post
Select Schedule Time (Image by Author)
  • Create Action — Click next after selecting the execution time. In this step, the scheduler will prompt you to select the action you want to schedule. Select “Start a program”. The interface will look like below:
Image for post
Image for post
Select Action (Image by Author)
  • Select Batch File — Click next after selecting the desired action. Now, the application interface will ask for the location of the program you want to schedule. Select the batch file which we had created in the previous section. The application screen will look as follows:
Image for post
Image for post
Select Batch File Location (Image by Author)
  • Finally — That’s it, click next and finish. Your Python script will execute at a specific time every day.

Closing note

I am sure that with a solution like above, you can automate all your repetitive tasks.

I hope this tutorial was informative and you learned something new. Please comment and share your feedback.

Stay tuned for more interesting topics for next time. Till then:

HAPPY LEARNING ! ! ! !


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK