【发布时间】:2020-01-16 09:05:53
【问题描述】:
我制作了一个监控脚本,它将读取实时更新的文本文件。当我运行脚本时,它会中途停止并返回
(PermissionError: [Errno 13] Permission denied: 'C:\txt LOG\DATA_LOG\DATA_LOG0.txt')
错误。
{"VarName": "C_SPREADER_45_FEET_SIGNAL", "TimeString": "2019-09-15 13:17:58", "VarValue": "0"}
{"VarName": "D_LOADCELL2", "TimeString": "2019-09-15 13:17:58", "VarValue": "480"}
{"VarName": "D_SPREADER_SIZE", "TimeString": "2019-09-15 13:17:58", "VarValue": "20"}
{"VarName": "C_SPREADER_40_FEET_SIGNAL", "TimeString": "2019-09-15 13:17:58", "VarValue": "0"}
{"VarName": "D_LOADCELL4", "TimeString": "2019-09-15 13:17:58", "VarValue": "379"}
{"VarName": "D_TOTAL_LOAD", "TimeString": "2019-09-15 13:17:58", "VarValue": "0"}
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\watchdog\observers\api.py", line 199, in run
self.dispatch_events(self.event_queue, self.timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\watchdog\observers\api.py", line 368, in dispatch_events
handler.dispatch(event)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\watchdog\events.py", line 330, in dispatch
_method_map[event_type](event)
File "C:\Program Files\pythonMonitor\watchProg1.py", line 8, in on_modified
logfile=open(filepath+"\DATA_LOG0.txt","r",encoding='utf16')
PermissionError: [Errno 13] Permission denied: 'C:\\TXT LOG\\DATA_LOG\\DATA_LOG0.txt'
我的代码:
import time
import json
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
filepath = ("C:\TXT LOG\DATA_LOG")
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
with open(filepath+"\DATA_LOG0.txt", "r", encoding='utf16') as logfile:
f = logfile.readlines()[1:]
logfile.close()
result = {}
for row in f:
word1 = row.strip().replace("\t", ",").replace('\"', '').split(',')
result = {'VarName': word1[0],'TimeString': word1[1], 'VarValue': word1[2]}
myjason = json.dumps(result)
print(myjason)
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path=filepath, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
{"VarName": "C_SPREADER_45_FEET_SIGNAL", "TimeString": "2019-09-15 13:17:58", "VarValue": "0"}
{"VarName": "D_LOADCELL2", "TimeString": "2019-09-15 13:17:58", "VarValue": "480"}
{"VarName": "D_SPREADER_SIZE", "TimeString": "2019-09-15 13:17:58", "VarValue": "20"}
{"VarName": "C_SPREADER_40_FEET_SIGNAL", "TimeString": "2019-09-15 13:17:58", "VarValue": "0"}
{"VarName": "D_LOADCELL4", "TimeString": "2019-09-15 13:17:58", "VarValue": "379"}
{"VarName": "D_TOTAL_LOAD", "TimeString": "2019-09-15 13:17:58", "VarValue": "0"}
【问题讨论】:
-
我猜日志应用程序在更新文件时会锁定文件。
-
也许我会尝试将日志文件写入另一个文本文件
标签: python python-3.x watchdog