提问者:小点点

permissionerror errno 13在监视程序监视读取文本中拒绝了权限


我做了一个监控脚本,它将读取实时更新文本文件。当我运行脚本时,它中途停止并返回

(PermissionError:[Errno 13]权限被拒绝:'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"}

共1个答案

匿名用户

问题是,无论活动是否完成,都会发生文件创建\修改事件。当前几个字节到达目录时,将触发事件。您可以添加以下剪下的部分,等待操作完成。

init_size = -1
while True:
    current_size = os.path.getsize(event.src_path)
    if current_size == init_size:
        break
    else:
        init_size = os.path.getsize(event.src_path)
        time.sleep(2)
print("file copy has now finished")