Windows shell中的“Bootstrap”python脚本,没有.py / .pyw关联

有时(在客户的PC中)我需要一个 python脚本在 Windows shell中执行,如.CMD或.BAT,但没有与PYTHON / PYTHONW关联的.py或.pyw扩展名.

我推出了一对’quick’n dirty’解决方案:

1)

"""
e:\devtool\python\python.exe %0 :: or %PYTHONPATH%\python.exe
goto eof:
""" 
# Python test
print "[works,but shows shell errors]"

2)

@echo off
for /f "skip=4 delims=xxx" %%l in (%0) do @echo %%l | e:\devtools\python\python.exe
goto :eof
::----------

# Python test
print "[works better,but is somewhat messy]"

你知道更好的解决方案吗? (即:更简洁或优雅)

更新:

基于@van回答,我找到了更简洁的方法(没有设置ERRORLEVEL)

@e:\devtools\python\python.exe -x "%~f0" %* & exit /b

### Python begins....
import sys

for arg in sys.argv:
    print arg

raw_input("It works!!!\n")

###
你可以尝试创建一个python和windows shell脚本的脚本.在这种情况下,您可以将文件命名为my_flexible_script.bat并直接或通过python执行….

查看pylint中pylint.bat文件的内容:

@echo off
rem = """-*-Python-*- script
rem -------------------- DOS section --------------------
rem You could set PYTHONPATH or TK environment variables here
python -x "%~f0" %*
goto exit

"""
# -------------------- Python section --------------------
import sys
from pylint import lint
lint.Run(sys.argv[1:])


DosExitLabel = """
:exit
exit(ERRORLEVEL)
rem """

它与您的操作类似,但具有更多兼容的双脚本支持.

相关文章

文章浏览阅读2.2k次,点赞6次,收藏20次。在我们平时办公工作...
文章浏览阅读1k次。解决 Windows make command not found 和...
文章浏览阅读3.2k次,点赞2次,收藏6次。2、鼠标依次点击“计...
文章浏览阅读1.3w次。蓝光版属于高清版的一种。BD英文全名是...
文章浏览阅读974次,点赞7次,收藏8次。提供了更强大的功能,...
文章浏览阅读1.4w次,点赞5次,收藏22次。如果使用iterator的...