How to insert python code in a bat file?

为君一笑 提交于 2020-01-02 08:44:20

问题


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).


回答1:


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




回答2:


Another solution is, in one line is :

'''
@echo off & python -x "%~f0" %* & goto :eof
print "Hello 123"
'''


来源:https://stackoverflow.com/questions/41642012/how-to-insert-python-code-in-a-bat-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!