有什么方法可以延迟 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?
我可能可以运行一个while循环来检查notepad.exe
是否是运行ning,如果是,如果不是,它应该跳出循环并执行其余代码。
但是,问题是如何检查 notepad.exe
是否为 运行ning?
运行一个while循环删除文件,如果get出错,说明程序还在运行ning,但是如果没有get就是问题了错误,它将删除文件。
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
我想要 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?
我可能可以运行一个while循环来检查
notepad.exe
是否是运行ning,如果是,如果不是,它应该跳出循环并执行其余代码。
但是,问题是如何检查notepad.exe
是否为 运行ning?运行一个while循环删除文件,如果get出错,说明程序还在运行ning,但是如果没有get就是问题了错误,它将删除文件。
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