[英]Passing arguments to python script from batch file
我想將參數從test.bat
傳遞給myscript.py
。
test.bat
:
../path/to/file/myscript.py "myarg1" "myarg2"
myscript.py
:
#!/usr/bin/env python2
import sys
if __name__ == '__main__':
print sys.version()
myarg1= sys.argv[1]
myarg2= sys.argv[2]
我正在使用Windows 10並將pylauncher配置為執行python文件的默認程序。 我的用戶和系統PATH
變量中都有python 2.7和3.6。 shebang會得到照顧,並按預期選擇py2.7解釋器。
我面臨的錯誤是未傳遞參數。 錯誤日志如下所示:
2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:22:17) [MSC v.1500 32 bit (Intel)]
Traceback (most recent call last):
File "C:\full\path\to\file\myscript.py", line 12, in <module>
myarg1= sys.argv[1]
IndexError: list index out of range
一種解決方案:當我在.bat文件中指定py
,它可以工作並且參數可以正確讀取
py ../path/to/file/myscript.py "myarg1" "myarg2"
如何在不將py
添加到我的.bat腳本的情況下使其工作?
更多信息-在cmd
shell中運行這些cmd
:
assoc .py
給出.py=Python.File
ftype Python.File
提供了Python.File="C:\\WINDOWS\\py.exe" "%L" %*
你必須對文件名末尾的.py為了這個工作。 您的test.bat文件應如下所示:
python myscript.py [arg1] [arg2]
如果myscript.py文件不在同一個目錄,然后把路徑myscript.py 結尾的 myscript.py。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.