Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert python code in a bat file?

Would like to insert python code or run ipython code in a windows bat file (Not in a separate .py file).

After searching, could not find any solution.

EDIT: This is for ipython code (not python).

like image 680
quantCode Avatar asked Jan 04 '23 18:01

quantCode


1 Answers

Try this (batch-python polyglot script ):

0<0# : ^
''' 
@echo off
echo batch code
python %~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

like image 73
npocmaka Avatar answered Jan 14 '23 04:01

npocmaka