windows batch脚本中嵌入python程序

来源:互联网 发布:淘宝动漫周边店 编辑:程序博客网 时间:2024/05/17 06:36
1>2# : ^
'''@echo offpython "%~f0"exit /brem ^'''import osprint "This is Python code"


具体解释参考以下英文:

The batch code is in a multiline string ''' so this is invisible for python.
The batch parser doesn't see the python code, as it exits before.

The first line is the key.
It is valid for batch as also for python!
In python it's only a senseless compare 1>2 without output, the rest of the line is a comment by the #.

For batch 1>2# is a redirection of stream 1 to the file 2#.
The command is a colon : this indicates a label and labeled lines are never printed.
Then the last caret simply append the next line to the label line, so batch doesn't see the ''' line.


此段英文解释摘自:https://stackoverflow.com/questions/17467441/how-to-embed-python-code-in-batch-script