[英]How do I pass variable command line arguments for a python script, that runs from a batch file?
I want to start my python script from a batch file. 我想从批处理文件启动我的python脚本。 It requires four arguments, that are processed in the script through sys.argv[n].
它需要四个参数,这些参数在脚本中通过sys.argv [n]处理。 These arguments are paths, that are varying from programme call to programme call.
这些参数是路径,在程序调用之间会有所不同。 So how do I create a batch file, where you can transfer new arguments for the programme call everytime you run the code?
那么,如何创建一个批处理文件,您可以在每次运行代码时为程序调用传递新的参数? I have learned Python for just one semester and am stuck with this problem.
我只花了一个学期就学习了Python,并被这个问题困扰。
Initially I have created a batchfile to test whether the code is even trying to be executed. 最初,我创建了一个批处理文件来测试代码是否甚至试图执行。 Of course it ran an error due to the arguments missing.
当然,由于缺少参数,它发生了错误。 Then I tried to pass the arguments through the batchfile, which didn't work and gave me the error like before 'list index out of range':
然后我尝试将参数传递给批处理文件,该文件不起作用,并给了我类似“列表索引超出范围”之前的错误:
set 1= path1
set 2= path2
set 3= path3
set 4= path4
"C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe" "C:\repo\PythonProgramme.py" %1 %2 %3 %4
pause
I expected the code to run, but as already said, the aguments where not being passed. 我希望代码可以运行,但是正如已经说过的那样,这些参数没有被传递。 Can somebody help me fix this issue and maybe give me a hint, what to use for varying arguments (paths)?
有人可以帮我解决这个问题,也许可以给我一个提示,该如何使用各种参数(路径)?
You need to surround your variable name with %
for it to work. 您需要在变量名前加上
%
才能起作用。 You should read the documentation for set
and maybe also this article about batch variables. 您应该阅读
set
的文档 ,也许还要阅读有关批处理变量的本文 。
In the meantime the following should work. 在此期间,以下应该工作。
set var1=path1
set var2=path2
set var3=path3
set var4=path4
"C:\[...]\python.exe" "C:\repo\PythonProgramme.py" %var1% %var2% %var3% %var4%
pause
The error you get is from your (unknown) python code. 您收到的错误来自您的(未知)Python代码。
%1%
%1%
%1..%4
you reference the command line args passed to the batch itself, %1..%4
您引用传递给批处理本身的命令行args, While variables names starting with a letter are preferable (as Jacques Gaudin suggests +1), you can try: 尽管最好使用以字母开头的变量名称(如雅克·高丁 ( Jacques Gaudin)建议的+1),但您可以尝试:
"C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe" ^
"C:\repo\PythonProgramme.py" %1% %2% %3% %4%
or, if the Path'es set may contain spaces: 或者,如果路径的集合可能包含空格:
"C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe" ^
"C:\repo\PythonProgramme.py" "%1%" "%2%" "%3%" "%4%"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.