国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Python持續(xù)監(jiān)聽文件變化代碼實例

瀏覽:8日期:2022-07-16 15:49:40

在日常的工作中,有時候會有這樣的需求,需要一個常駐任務(wù),持續(xù)的監(jiān)聽一個目錄下文件的變化,對此作出回應(yīng).

pyinotify就是這樣的一個python包,使用方式如下:

一旦src.txt有新的內(nèi)容,程序就可以監(jiān)控到,然后將內(nèi)容發(fā)送

import socketimport pyinotifypos = 0def send(c): c_list = [c] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((’127.0.0.1’, 10001)) print(s.recv(1024).decode(’utf-8’)) for data in c_list: s.send(data) print(s.recv(1024).decode(’utf-8’)) s.send(b’exit’) s.close()def printlog(): global pos try: fd = open('src.txt') if pos != 0: fd.seek(pos, 0) while True: line = fd.readline() if line.strip():send(line.strip().encode(’utf8’)) pos = pos + len(line) if not line.strip():break fd.close() except Exception as e: print(str(e))class MyEventHandler(pyinotify.ProcessEvent): # 當文件被修改時調(diào)用函數(shù) def process_IN_MODIFY(self, event): try: printlog() except Exception as e: print(str(e))if __name__ == ’__main__’: printlog() # watch manager wm = pyinotify.WatchManager() wm.add_watch(’/home/ubuntu/data-sync/s3’, pyinotify.ALL_EVENTS, rec=True) eh = MyEventHandler() # notifier notifier = pyinotify.Notifier(wm, eh) notifier.loop()

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 巴青县| 毕节市| 隆尧县| 新绛县| 大名县| 新源县| 兴国县| 泰安市| 米脂县| 西昌市| 万载县| 浦城县| 宕昌县| 牙克石市| 贺兰县| 濮阳市| 农安县| 古蔺县| 延庆县| 彰武县| 宜昌市| 静海县| 辽阳县| 新民市| 望江县| 道孚县| 东至县| 邓州市| 如皋市| 文化| 象州县| 盐源县| 海林市| 蒙城县| 澜沧| 鹿邑县| 八宿县| 中宁县| 开化县| 孝感市| 博兴县|