Python - 使用 Popen 时 for 循环意外中断

Python - Unexpected break in for-loop while using Popen

我需要使用不同的参数多次调用一个脚本。参数存储在 argument_list 及其字符串列表中。

for argument in argument_list:
    python_command = "python another_script.py --server " + argument
    p = Popen(python_command,shell=True, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()

问题是在一次迭代后,脚本会从 for 循环中中断,即使 argument_list 中仍有项目并且循环中没有中断。

有谁知道为什么会这样,我该如何解决?感谢您的任何建议。

python_command = "python another_script.py --server " + argument

Popen的参数必须按顺序排列:

python_command = ["python", "another_script.py", "--server", argument]

另外,一个建议:尽量把Popen语句放在一个try ... except中,这样如果有错误,你会得到更详细的原因解释。