How to Run Python Script Using Batch File

run-python-script-using-batch-commands-simplemsoffice

This batch file code will help to execute the python script file from any folder location. There is no need to run the python script manually when multiple python scripts are required to run. All these can be run by batch file in a single instance. And batch file can also run multiple python scripts from different folders or locations.

Below is the step-by-step guide to let you create your batch file.

Step 1: Copy the Python Script file into the raw data folder.

For example: “C:\Source Folder\” is the raw data folder, so copy the script file in this folder.

Step 2: Open Notepad in your system.

Step 3: Copy the below code in Notepad:

1
2
set env=python.exe
start PythonScript.py

Step 4: Save the Notepad file in the “Source Folder” and give any name like “RunPython.bat“.

Now you can run the batch file and it will run the python script.

Note:- If you want to edit the batch file, then right-click on it and then click on Edit or you can open the batch file in Notepad and after editing you can save it.

run-python-using-batch-file-simplemsoffice

If you want to save the batch file in other locations then add the below code.

This command changes the folder location or you can say enter the folder and run the script.

1
cd "C:\Source Folder"

So it will become:

1
2
3
cd "C:\Source Folder"
set env=python.exe
start PythonScript.py

 

If you want to run the multiple python scripts from different folders,

then add code multiple times for all folders like below:

This command will execute all scripts at a time and after completion batch file, it will close automatically.

1
2
3
4
5
6
7
8
9
10
11
cd "C:\Source Folder"
set env=python.exe
start PythonScript.py

cd "C:\Source Folder 1"
set env=python.exe
start PythonScript.py

cd "C:\Source Folder 2"
set env=python.exe
start PythonScript.py

If you want to run python scripts one after another in serial or folder wise
Then add “pause” before every set of commands like the below:

In every instance, the batch file will ask to press any key to continue to go and run the next set of commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pause
cd "C:\Source Folder"
set env=python.exe
start PythonScript.py

pause
cd "C:\Source Folder 1"
set env=python.exe
start PythonScript.py

pause
cd "C:\Source Folder 2"
set env=python.exe
start PythonScript.py

pause

 

0 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *