Creating a BAT file for python script Creating a BAT file for python script python python

Creating a BAT file for python script


c:\python27\python.exe c:\somescript.py %*


Open a command line (⊞ Win+R, cmd, ↵ Enter)and type python -V, ↵ Enter.

You should get a response back, something like Python 2.7.1.

If you do not, you may not have Python installed. Fix this first.

Once you have Python, your batch file should look like

@echo offpython c:\somescript.py %*pause

This will keep the command window open after the script finishes, so you can see any errors or messages. Once you are happy with it you can remove the 'pause' line and the command window will close automatically when finished.


Here's how you can put both batch code and the python one in single file:

0<0# : ^''' @echo offecho batch codepython "%~f0" %*exit /b 0'''print("python code")

the ''' respectively starts and ends python multi line comments.

0<0# : ^ is more interesting - due to redirection priority in batch it will be interpreted like :0<0# ^ by the batch script which is a label which execution will be not displayed on the screen. The caret at the end will escape the new line and second line will be attached to the first line.For python it will be 0<0 statement and a start of inline comment.

The credit goes to siberia-man