1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #!/usr/bin/env python # coding=utf-8 import os import sys import time import win32file import win32con import subprocess
def TimeStampToTime(timestamp): timeStruct = time.localtime(timestamp) return time.strftime('%Y-%m-%d/%H:%M:%S',timeStruct)
ACTIONS = { 1: "Created", 2: "Deleted", 3: "Updated", 4: "Renamed from something", 5: "Renamed to something" }
temptask = [] FILE_LIST_DIRECTORY = 0x0001 path_to_watch = 'c:/test' print ('Watching changes in', path_to_watch) hDir = win32file.CreateFile( path_to_watch, FILE_LIST_DIRECTORY, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, None, win32con.OPEN_EXISTING, win32con.FILE_FLAG_BACKUP_SEMANTICS, None ) while 1: results = win32file.ReadDirectoryChangesW( hDir, 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 results: full_filename = os.path.join(path_to_watch, filename) print (full_filename, ACTIONS.get(action, "Unknown")) if ACTIONS.get(action, "Unknown") == "Deleted": message = full_filename, ACTIONS.get(action, "Unknown") message1 = str(message) task = message1.split(" ") subprocess.call(['python','weixin.py',task]) else: try: filesize = os.path.getsize(full_filename) filesize = filesize/float(1024*1024) filesize = str(round(filesize,2)) + "MB" filetime = os.path.getctime(full_filename) tt = TimeStampToTime(filetime) message = tt,full_filename, ACTIONS.get(action, "Unknown"),filesize message1 = str(message) task = message1.split(" ") except Exception as e: continue # message = full_filename, ACTIONS.get(action, "Unknown") # message1 = str(message) # task = message1.split(" ") # subprocess.call(['python','weixin.py',task]) finally: if task != temptask: subprocess.call(['python','weixin.py',task]) temptask = task
|