{"id":663,"date":"2025-05-07T09:51:04","date_gmt":"2025-05-07T01:51:04","guid":{"rendered":"https:\/\/znl.pub\/?p=663"},"modified":"2025-05-07T09:51:04","modified_gmt":"2025-05-07T01:51:04","slug":"python%e5%88%a9%e7%94%a8watchdog%e7%9b%91%e6%8e%a7%e7%9b%ae%e5%bd%95%e6%96%87%e4%bb%b6%e5%88%9b%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/znl.pub\/?p=663","title":{"rendered":"Python\u5229\u7528watchdog\u76d1\u63a7\u76ee\u5f55\u6587\u4ef6\u521b\u5efa"},"content":{"rendered":"\n<p>\u9700\u6c42\u662f\u8981\u5b9e\u73b0\u81ea\u52a8\u76d1\u63a7\u4e0a\u4f20\u76ee\u5f55\uff0c\u4e00\u65e6\u6709\u6307\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\u4f20\u5b8c\uff0c\u5c31\u8c03\u7528\u5176\u4ed6\u8fdb\u7a0b\u5904\u7406\u8fd9\u4e2a\u6587\u4ef6\u3002\u8fd9\u91cc\u4e0d\u80fd\u91c7\u53d6\u904d\u5386\u76ee\u5f55\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u4f1a\u83b7\u53d6\u5230\u6ca1\u6709\u4e0a\u4f20\u5b8c\u6bd5\u7684\u6587\u4ef6\u3002\u56e0\u4e3a\u83b7\u53d6\u6587\u4ef6\u540d\u4e0e\u5199\u6587\u4ef6\u4e0d\u662f\u4e92\u65a5\u64cd\u4f5c\uff0c\u6ca1\u6709\u539f\u5b50\u6027\u3002\u8fd9\u5f53\u7136\u53ef\u4ee5\u901a\u8fc7\u5148\u4f20\u540e\u6539\u540e\u7f00\u540d\u7684\u65b9\u5f0f\u907f\u514d\u83b7\u53d6\u5230\u672a\u4f20\u5b8c\u7684\u6587\u4ef6\uff08\u56e0\u4e3amv\u64cd\u4f5c\u662f\u539f\u5b50\u64cd\u4f5c\uff09\u3002<\/p>\n\n\n\n<p>\u4e0d\u8fc7python\u6709\u4e2a\u597d\u7528\u7684\u5e93\u53ebwatchdog\u53ef\u4ee5\u505a\u5230\u6587\u4ef6\u7cfb\u7edf\u4e8b\u4ef6\u76d1\u542c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python\nimport time\nfrom watchdog.observers import Observer\nfrom watchdog.events import FileSystemEventHandler\n\nclass EmlHandler(FileSystemEventHandler):\n    def on_created(self, event):\n        if event.src_path.endswith(\".eml\"):\n            print(event.src_path)\n          # process_it(event.src_path)\n\ndef start_monitoring(path_to_watch):\n    event_handler = EmlHandler()\n    observer = Observer()\n    observer.schedule(event_handler, path=path_to_watch, recursive=False)\n    observer.start()  # \u542f\u52a8\u89c2\u5bdf\u8005\u7ebf\u7a0b\n    try:\n        print(f\"\u5f00\u59cb\u76d1\u63a7\u76ee\u5f55: {path_to_watch}\")\n        while True:  # \u4e3b\u7ebf\u7a0b\u4fdd\u6301\u8fd0\u884c\n            time.sleep(1)\n    except KeyboardInterrupt:\n        observer.stop()\n    observer.join()\n\nif __name__ == \"__main__\":\n    start_monitoring(\".\/data\")  # \u4fee\u6539\u4e3a\u5b9e\u9645\u76d1\u63a7\u76ee\u5f55<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\u662f\u8981\u5b9e\u73b0\u81ea\u52a8\u76d1\u63a7\u4e0a\u4f20\u76ee\u5f55\uff0c\u4e00\u65e6\u6709\u6307\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\u4f20\u5b8c\uff0c\u5c31\u8c03\u7528\u5176\u4ed6\u8fdb\u7a0b\u5904\u7406\u8fd9\u4e2a\u6587\u4ef6\u3002\u8fd9\u91cc\u4e0d\u80fd\u91c7\u53d6\u904d\u5386\u76ee\u5f55\u7684\u65b9\u5f0f&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-663","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=663"}],"version-history":[{"count":1,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/663\/revisions"}],"predecessor-version":[{"id":664,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/663\/revisions\/664"}],"wp:attachment":[{"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}