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")
You can try somthing like this...
while 1:
try:
'''
Your code
'''
except KeyboardInterrupt as exception:
break
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.