使用python在osx 10.8.2中进行文件监视

时间:2013-10-14 07:33:32

标签: python multithreading macos file-io win32com

我正在使用文件监视器来使用win32file监听器来监听文件中的更改。以下代码在Windows上运行正常。此代码不适用于mac osx。有关在mac osx上制作文件监视器的任何建议。

thread1 = myThread(1, tempFileName, tempLocation,selectedFileName)
thread1.start();    

    class myThread (threading.Thread):
                def __init__(self, threadID, fileName, directory, origin):
                    threading.Thread.__init__(self)
                    self.threadID = threadID
                    self.fileName = fileName
                    self.daemon = True
                    self.dir = directory
                    self.originalFile = origin
                def run(self):
                    startMonitor(self.fileName, self.dir, self.originalFile)

    def startMonitor(fileMonitoring,dirPath,originalFile):
                    hDir = win32file.CreateFile (
                      dirPath,
                      FILE_LIST_DIRECTORY,
                      win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
                      None,
                      win32con.OPEN_EXISTING,
                      win32con.FILE_FLAG_BACKUP_SEMANTICS,
                      None
                    )
                    readFlags = 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
                    # Wait for new data and call ProcessNewData for each new chunk that's    written
                    while 1:
                        # Wait for a change to occur
                        results = win32file.ReadDirectoryChangesW (
                                                                   hDir,
                                                                   1024,
                                                                   False,
                                                                   readFlags,
                                                                   None
                                                                   )
                        # For each change, check to see if it's updating the file we're interested in
                        for action, file_M in results:
                            full_filename = os.path.join (dirPath, file_M)
                            #print file, ACTIONS.get (action, "Unknown")
                            if len(full_filename) == len(fileMonitoring) and action == 3:
                                if os.path.exists(originalFile):
                                        encrypt_file(fileMonitoring,originalFile)

2 个答案:

答案 0 :(得分:1)

pywin32 (Python extensions for Windows)仅适用于Windows,顾名思义。

您可以使用watchdog。它支持Mac OS X以及Linux,Windows,FreeBSD。

答案 1 :(得分:0)

你可以用它。它适用于我的情况。

class ChangeHandler(FileSystemEventHandler):

        def on_any_event(self, event):
            encrypt_file(key, tempFileName, selectedFileName,iv)

    def filemonitor(tempFileName,tempLocation):

        while 1:

            event_handler = ChangeHandler()
            observer = Observer()
            observer.schedule(event_handler, tempLocation, recursive=True)
            observer.start()
            try:
                while True:
                    time.sleep(1)
            except KeyboardInterrupt:
                observer.stop()
            observer.join()        

    Thread(target=filemonitor,args=(tempFileName,tempLocation,)).start()