Sajal Rastogi
2 min readAug 3, 2020

--

Running Python Script from Batch File

Hello Medium,

This article will help you to get insights of running python script directly with one click . This will be specially be helpful when you are using automation bots. This method can also be used instead of exe files in development phase as they can’t be edited easily .

You can find the code —github.com/r-sajal/PythonRunner

Lets begin our discussion with some terms used in code :

batch file — A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file.

echo → Print stuff on command prompt.
on → To show what are we outputting
& → First performs the task on left and then the task on right
python3 → Global variable for python
-x → Version
“%~f0”% — Represents a single letter replaceable variable
f0 — To get the complete path of the file (python in our case)
~ — To remove double quotes if any
goto — To go to the defined label
:eof — End of file (predefined label)

echo on & python3 -x "%~f0" %* & goto :eof # ========================================================== #
# Save File as bat file
# ========================================================== #
# Example Python Code -->
import module
x = input()
print(x)
y = input()

Make Sure to save This file with .bat extension .

Input in last line is holding the screen so that user can view output. If you are showing outputs you have to put input at last else cmd will close in an instant.

Useful references for learning more about batch files — ss64.com

Thank You Readers !

--

--