Python 无法使用subprocess.Popen()运行进程
我有一个从python程序运行的命令Python 无法使用subprocess.Popen()运行进程,python,multiprocessing,Python,Multiprocessing,我有一个从python程序运行的命令 python test.py arg1 arg2 如果我使用 os.system("python test.py arg1 arg2") 它运行 我想使用subprocess.Popen()但是当我这样做时 subprocess.Popen("python test.py arg1 arg2") 它给出了以下错误 raise child_exception OSError: [Errno 2] No such file or directory 有
python test.py arg1 arg2
如果我使用
os.system("python test.py arg1 arg2")
它运行
我想使用subprocess.Popen()但是当我这样做时
subprocess.Popen("python test.py arg1 arg2")
它给出了以下错误
raise child_exception
OSError: [Errno 2] No such file or directory
有什么想法吗
非常感谢试着把你所有的论点都列在一个列表中:
subprocess.Popen(["python", "test.py", "arg1", "arg2"])
请参阅:尝试将所有参数放入列表中:
subprocess.Popen(["python", "test.py", "arg1", "arg2"])
请参阅:如果
Popen
的参数是字符串,它会尝试将其作为'sh'
执行,因此在您的示例中,它会变成'sh python test.py arg1 arg2'
,这显然是错误的。您可以向它传递一个列表(正如twall所建议的),或者指定shell=True
参数。下一步应该是:
subprocess.Popen("python test.py arg1 arg2", shell=True)
如果
Popen
的参数是字符串,它会尝试将其作为'sh'
执行,因此在您的示例中,它会变成'sh python test.py arg1 arg2'
,这显然是错误的。您可以向它传递一个列表(正如twall所建议的),或者指定shell=True
参数。下一步应该是:
subprocess.Popen("python test.py arg1 arg2", shell=True)
谢谢你的回答。。。它是有效的。。。但是上面的一个最适合我的要求谢谢你的回答。。。它是有效的。。。但是上面这一个最适合我的要求