Python os.startfile()没有time.sleep

时间:2018-03-29 12:33:53

标签: python python-3.x

目前我正在使用以下代码从我的Python脚本中打开文件,顺便提一下我启动一个程序(.exe文件):

os.startfile(filePath)
time.sleep(20)

但有没有办法可以在不使用time.sleep(20)等待文件打开的情况下执行此操作?

1 个答案:

答案 0 :(得分:1)

来自文档:

  一旦启动关联的应用程序,

startfile()就会返回。没有选项可以等待应用程序关闭,也无法检索应用程序的退出状态。

请改为subprocess,尤其是subprocess.check_call,这将等到流程终止。

import subprocess

subprocess.check_call(['start', filePath])