python - 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'
解决方案
使用subprocess.run
而不是subprocess.call
. 它正在阻塞,所以不需要wait
.
https://docs.python.org/fr/3/library/subprocess.html
推荐阅读
- reactjs - 如何根据条件逻辑触发 RxJS 中的操作?
- typescript - 如何为其他用户创建模型
- winapi - 为什么来自 MS CryptoAPI 的 CryptDecrpyt() 不能正确解密远程系统上的前 16 个字节?
- css - 在 ionic 5 中设置背景图像
- reactjs - Create-react-app 绝对导入不适用于 yarn v2 工作区 + 打字稿
- mysql - 将多条记录的左连接表合并为一条记录mysql
- scala - 具有结构共享的数据结构转换
- java - 在 PreparedStatement 处获取 NullPointerException
- python - 从文件中读取文本并在 if 语句中使用
- node.js - 为什么模式字段没有使用节点 js 保存在数据库中