简体   繁体   English

如何为从批处理文件运行的python脚本传递变量命令行参数?

[英]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代码。

  • While unusual you can set variable names with just numbers 虽然不常见,但您可以只用数字设置变量名
  • BUT to reference these vars you need to enclose them in percent signs on both sides %1% 但是要引用这些变量,您需要将其两侧都用百分号括起来%1%
  • With %1..%4 you reference the command line args passed to the batch itself, 对于%1..%4您引用传递给批处理本身的命令行args,
    if nothing was passed, there are no args for python to process 如果没有传递任何内容,则没有用于python的参数
  • spaces around the equal sign in a set become part of the variable name/content. 集合中等号周围的空格成为变量名称/内容的一部分。

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.

相关问题 Python 脚本 - 如何从命令行参数将密码作为变量传递 - Python script - how to pass password as variable from command line arguments 如何从脚本将命令行参数传递给python文件 - How to pass command line arguments to a python file from a script 如何将 arguments 从命令行传递到 python 脚本以读取和更新 json 文件中存在的某些键的值 - How do I pass arguments from command line to the python script to read and update values of certain keys present in a json file 如何将参数传递给从命令行启动的 python gdb 脚本 - How to pass arguments to a python gdb script launched from command line 来自文件的python脚本命令行参数 - python script command line arguments from file 如何在调试模式下将命令行参数从 VS 传递给 Python? - How do I pass command line arguments to Python from VS in Debug mode? Python:如何从命令行 arguments 定义可由多处理池访问的全局变量? - Python: How do I define a global variable accessible by a multiprocessing pool from command line arguments? 将命令行参数传递给没有脚本文件的python解释器 - pass command line arguments to python interpreter without a script file 如何从另一个脚本中将命令行 arguments 传递给 python 脚本? - How to pass command-line arguments to a python script from within another script? Python脚本在命令行上运行,但不是从.sh文件运行 - Python script runs on command line but not from .sh file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM