ramanes ramalingam
ramanes ramalingam

Reputation: 23

permissionerror errno 13 permission denied in watchdog monitoring read text

I made a monitoring script which will read live updating text file. When i run the script, it stop halfway and return

(PermissionError: [Errno 13] Permission denied: 'C:\txt LOG\DATA_LOG\DATA_LOG0.txt')

error.

{"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'

My code:

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"}

Upvotes: 0

Views: 2295

Answers (1)

Basil Jose
Basil Jose

Reputation: 1044

The thing is that the file creation\modification event occurs irrespective of the activity completion. When the first few bytes come to the directory the events are being triggered. You may add the below snipped to wait till the operation to complete.

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")

Upvotes: 1

Related Questions