gpt4 book ai didi

python - 使用 N 个参数运行 subprocess.popen

转载 作者:太空宇宙 更新时间:2023-11-03 15:41:10 24 4
gpt4 key购买 nike

subprocess.popen 在后台调用进程。

我正在调用 php 进程,有时调用 python 进程,每次参数可能不同。我想编写一个函数,并在其中传递参数列表。该函数将根据参数执行进程。

所以我正在做的是,写在下面,这实际上是很糟糕的方式,我不想写这么多行。

    def RunBackGroundProcess(args):
# args is an array of params
# index 0 - command - php/python or anyother
# index 1 - filename - that you want to run. Specify fullpath if required
# index 2 - param_1
# index 3 - param_2
# index N - param_N

if(len(args) == 2):
subprocess.Popen([args[0], args[1], args[2]])

if(len(args) == 3):
subprocess.Popen([args[0], args[1], args[2], args[3]])

if(len(args) == 4):
subprocess.Popen([args[0], args[1], args[2], args[3], args[4]])

如果我有 10 个参数或 15 个参数怎么办?这是个糟糕的方法,对吧?帮助我用更小的行编写这段代码。

最佳答案

您将参数作为列表传递,因此虽然 subprocess.Popen() 不需要检查参数长度,您可以直接传递给子进程

def RunBackGroundProcess(args):
subprocess.Popen(args)

这会起作用

关于python - 使用 N 个参数运行 subprocess.popen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42104823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com