我看过多篇关于传递字符串的帖子,但无法找到从批处理文件中读取传递给 python 脚本的字符串的好的解决方案。这是我的问题。我正在从批处理文件调用 python 脚本并传递参数。string_var = "123_Asdf"bat 'testscript.py %string_var%'我在我的 python 代码中有以下内容。import syspassed_var = sys.argv[1]当我运行上面的代码时,我总是看到下面的错误。passed_var = sys.argv[1]IndexError: list index out of range有没有人见过这个问题?我只传递字符串并希望它作为我传递给脚本的第一个参数的一部分被读取。
3 回答

互换的青春
TA贡献1797条经验 获得超6个赞
如果我已正确阅读您的问题及其格式,我认为您的.bat文件应为:
Set string_var="123_Asdf"
"D:\BuildTools\tools\python27\python.exe" testscript.py %string_var%
或者更好:
Set "string_var=123_Asdf"
"D:\BuildTools\tools\python27\python.exe" testscript.py "%string_var%"
%string_var%可以带或不带双引号传递where 。
添加回答
举报
0/150
提交
取消