![](/img/trans.png)
[英]How to stop inifinite loop with specific key instead of keyboard Interrupt exception?
[英]how to stop that while 1 loop by using key interrupt?
import os
import win32file
import win32con
ACTIONS = {
1 : "Created",
}
FILE_LIST_DIRECTORY = 0x0001
path_to_watch = "."
hDir = win32file.CreateFile (
path_to_watch,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None
)
try:
while 1:
print("file monitoring started")
results = win32file.ReadDirectoryChangesW (
hDir,
1024,
True,
win32con.FILE_NOTIFY_CHANGE_FILE_NAME ,
None,
None
)
for action, file in results:
full_filename = os.path.join (path_to_watch, file)
if action == 1:
print (full_filename, ACTIONS.get (action, "Unknown"))
except KeyboardInterrupt:
print("exited")
你可以試試這樣的...
while 1:
try:
'''
Your code
'''
except KeyboardInterrupt as exception:
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.