有时(在客户的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执行….
- @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 """
它与您的操作类似,但具有更多兼容的双脚本支持.