Wednesday, January 30, 2019

How to Run Python Script Using Batch File - Simple Step by Step Guide




Condition: Python should be installed in your system


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

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


Step 2: Open Notepad in your system.


Step 3: Copy below code in Notepad:


set env=python.exe

start PythonScript.py


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


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


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




If you want to save batch file in other location then add below code:


‘This command changes the folder location or you can say enters in the folder and run script.

cd "C:\Source Folder\"


So it will become:


cd "C:\Source Folder\"

set env=python.exe

start PythonScript.py




If you want to run script in different folders then add code multiple time for all folders like below:


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


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



If you want to run script one by one folder wise

Then add “pause” before every set of command like below:


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




***** End *****


1 comment: