TA的每日心情 | 郁闷 2024-5-22 11:47 |
---|
签到天数: 62 天 [LV.6]常住居民II
|
发表于 2016-12-23 13:45:49
|
显示全部楼层
import threading
import win32file
import win32con
import os
dirs_to_monitor = []
while True:
site=raw_input("Please inpue Monirot Site:")
# site.strip("\r").strip("\n")
if site=="ok":
break
dirs_to_monitor.append(site)
FILE_CREATED = 1
FILE_DELETED = 2
FILE_MIDIFIED = 3
FILR_RENAMED_FROM = 4
FILE_RENAMED_TO = 5
def start_monitor(path_to_watch):
FILE_LIST_DIRECTORY = 0x0001
h_directory =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
)
while True:
try:
result = win32file.ReadDirectoryChangesW(
h_directory,
1024,
True,
win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
win32con.FILE_NOTIFY_CHANGE_SIZE |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
win32con.FILE_NOTIFY_CHANGE_SECURITY,
None,
None
)
for action,filename in result:
full_filrname=os.path.join(path_to_watch,filename)
if action ==FILE_CREATED:
print "[ + ] Created %s" % full_filrname
elif action ==FILE_DELETED:
print "[ - ] Delete %s" % full_filrname
elif action==FILE_MIDIFIED:
print "[ * ] Modified %s" %full_filrname
print "[vvv] Dumping contents..."
try:
f=open(full_filrname,"rb")
contends= f.read()
f.close()
print contends
print "[^^^] Dump Complete."
except:
print "[!!!] Failed."
elif action==FILR_RENAMED_FROM:
print "[ > ] Renamed from: %s" %full_filrname
elif action==FILE_RENAMED_TO:
print "[ < ] Rename to: %s" %full_filrname
else:
print "[???] Unknown: %s" %full_filrname
except:
pass
for path in dirs_to_monitor:
montor_thread = threading.Thread(target=start_monitor,args=(path,))
print "Start Site Monitor Site:%s" % path
montor_thread.start()
python脚本一个,通过改第五十七行到第五十九行的内容可以实现要求,还有就是,依赖的库记得装 |
|