我希望 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正在運行,如果是,如果不是,它應該跳出回圈并執行其余的代碼。
但是,問題是如何檢查是否notepad.exe正在運行?運行while回圈洗掉檔案,如果報錯,說明程式還在運行,但問題是如果沒有報錯,就會洗掉檔案。
如果在啟動程式時獲取它的行程ID,并且只等待它終止,那就更好了。這樣記事本的其他實體不會受到影響。
uj5u.com熱心網友回復:
從檔案:
startfile() 在關聯的應用程式啟動后立即回傳。沒有等待應用程式關閉的選項,也沒有辦法檢索應用程式的退出狀態。
如果您知道打開檔案的應用程式的路徑,您可以使用 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325090.html
上一篇:如何使用FS回傳png檔案
