有什么方法可以延迟 Python 脚本直到应用程序为 运行?

Is there any way to delay a Python Script till an Application is running?

我想要 Python 代码打开记事本并让用户在其中输入内容,届时它不应执行其余代码。在我关闭记事本后,它应该从它离开的地方恢复脚本。有什么办法吗?或者我应该尝试不同的方法?

我尝试了什么:

with open('file.txt','w') as file: #this is to create an empty file
    file.close()
    pass
os.startfile('file.txt')
time.sleep() # what value should I enter for time.sleep? or is there a module to do this?

It would be better, if when launching of the program, it takes the process ID of it, and only wait for it to terminated. So that other instances of notepad won't be affected.

来自文档:

startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.

如果您知道用于打开文件的应用程序的路径,您可以使用 subprocess.Popen(),它允许您等待。

p = subprocess.Popen([
    'C:\Windows\System32\notepad.exe', 
    'path\to\file'
])

(output, err) = p.communicate()  

#This makes the wait possible
p_status = p.wait()

见: http://docs.python.org/library/os.html#os.startfile

http://docs.python.org/library/subprocess.html#subprocess.Popen