3

我在Mac上。我一直在使用 Launchd 的 WatchPaths 指令来监视目录中的文件更改。我的脚本仅在从监视目录中添加或删除文件时触发。

但是,修改文件时脚本不会触发..

本质上,我正在尝试创建一个 DIY Dropbox 来同步我的站点文件夹。

有没有办法通过launchd、bash或python来做到这一点?

我认为linux有类似inotify的东西,但我不知道mac的解决方案。

4

1 回答 1

2

我使用MacFSEvents 包(也可以在 PyPI 上找到)尝试解决这个问题:

import os

from fsevents import Observer, Stream


def callback(file_event):
    print file_event.name # the path of the modified file


def main():
    observer = Observer()
    observe_path = os.getcwd() # just for this example
    stream = Stream(callback, observe_path, file_events=True)
    observer.start()
    observer.schedule(stream)


if __name__ == '__main__':
    main()

这将callback在任何时候创建、修改或删除文件时调用(您可以使用 的值检查发生了哪个事件file_event.mask)。

请注意,您可能希望在主线程之外的线程上观察(上面的程序拒绝退出,即使在 上KeyboardInterrupt)。有关 API 的更多信息可以在 MacFSEvents README 中找到。希望这可以帮助!

于 2012-05-13T04:01:27.063 回答