Skip to main content
 首页 » 编程设计

python之无法使用 subprocess.Popen() 运行进程

2025年05月04日19birdshome

我有一个从 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 

有什么想法吗?

非常感谢

请您参考如下方法:

如果参数为 Popen是一个字符串,它会尝试以 'sh <argument>' 的形式执行它,所以在你的情况下它变成了 'sh python test.py arg1 arg2'这显然是错误的。您可以向它传递一个列表(如 twall 建议的那样),或指定 shell=True范围。下一个应该工作:

subprocess.Popen("python test.py arg1 arg2", shell=True)