2017-05-04 32 views
1

我想通过子进程运行cmd并将其传递给参数列表,其中一个参数是包含其他参数的列表。我怎样才能把它传递给子进程?Python子进程,如何传递列表?

例子:

my_list = ['arg1', 'arg2', 'arg3'] 
subprocess.run(["./some.sh", "--flag", "some_arg", "--another_flag", my_list ]) 

这可能吗?

回答

1

要么使用*解压名单:

subprocess.run(["./some.sh", "--flag", "some_arg", "--another_flag", *my_list]) 

还是两个清单+串连在一起:

subprocess.run(["./some.sh", "--flag", "some_arg", "--another_flag"] + my_list) 

+只能在工作列表(没有发电机,例如)。

*的“解包”行为记录在官方指南here中,尽管它只是在函数调用中使用它,而不是构造新列表。它无论如何工作。

+0

很好的解释,谢谢。 – user797963

相关问题