首页 > 解决方案 > Python:如何使用 subprocess.call 等待进程返回码

问题描述

我用subprocess.call这种方式用参数启动我的 exe 文件:

def run_file(self, file_path):
    subprocess.call([r'c:\file.exe', self.server_ip_address, file_path])

这工作正常,但现在我想等到我的return code.

所以我尝试使用这种方法:

def run_file(self, file_path):
    process = subprocess.call([r'c:\file.exe', self.server_ip_address, file_path])
    process.wait()
    print(process.returncode)

得到了这个error

process.wait() AttributeError: 'int' object has no attribute 'wait'

标签: pythonsubprocess

解决方案


使用subprocess.run而不是subprocess.call. 它正在阻塞,所以不需要wait.
https://docs.python.org/fr/3/library/subprocess.html


推荐阅读