更改文件后如何读取文件?
How to read the file when that file was changed?
不知道watchdog脚本和读文件脚本怎么一起用
import sys
import time
import logging from watchdog.observers
import Observer from watchdog.events
import LoggingEventHandler
if name == "main":
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
path = "C:/watch"
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
file = open('test.txt','r')
f = file.readlines()
newList = []
for line in f:
newList.append(line[:-1])
print(newList)
您的导入不正确(语法错误),if name == "main":
测试也不正确,再看一下 https://pypi.org/project/watchdog/ 中的示例。这是一个补丁版本,加上下面列出的一些更改:
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
def on_modified(event_handler):
print(f'event_handler.src_path={event_handler.src_path}')
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = "C:/watch"
event_handler = LoggingEventHandler()
event_handler.on_modified = on_modified
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
我添加了一个回调函数 on_modified
(目前只做一个打印),将其分配给您的事件处理程序的 on_modified 函数,删除了对“test.txt”文件的编辑结束。您现在应该将其放入回调函数中。
在回调函数中可以看到event_handler.src_path
是一个被修改的文件的路径:此时可以放入读取文件的代码
不知道watchdog脚本和读文件脚本怎么一起用
import sys
import time
import logging from watchdog.observers
import Observer from watchdog.events
import LoggingEventHandler
if name == "main":
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
path = "C:/watch"
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
file = open('test.txt','r')
f = file.readlines()
newList = []
for line in f:
newList.append(line[:-1])
print(newList)
您的导入不正确(语法错误),if name == "main":
测试也不正确,再看一下 https://pypi.org/project/watchdog/ 中的示例。这是一个补丁版本,加上下面列出的一些更改:
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
def on_modified(event_handler):
print(f'event_handler.src_path={event_handler.src_path}')
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = "C:/watch"
event_handler = LoggingEventHandler()
event_handler.on_modified = on_modified
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
我添加了一个回调函数 on_modified
(目前只做一个打印),将其分配给您的事件处理程序的 on_modified 函数,删除了对“test.txt”文件的编辑结束。您现在应该将其放入回调函数中。
在回调函数中可以看到event_handler.src_path
是一个被修改的文件的路径:此时可以放入读取文件的代码