Python:如何使用subprocess.call等待进程返回代码

问题描述 投票:0回答:1

我正在使用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'
python subprocess
1个回答
0
投票

使用subprocess.run而不是subprocess.call。它阻塞所以不需要waithttps://docs.python.org/fr/3/library/subprocess.html

© www.soinside.com 2019 - 2024. All rights reserved.