0

I'm trying to monitor a folder with Python3.
My program below (please bear with my messy code...) works perfectly fine on Mac, but it acts weird on PC.
It works as expected when I put the first image into the folder, but then I get PermissionError: [Errno 13] Permission denied error if I put the next image.

Does anyone have any idea why this is happening?

import time
import os
import mimetypes
import base64
import hashlib

from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

ACCEPTED_FILE_TYPES = [
    'image/tiff',
    'image/jpeg',
    'image/png'
]

hashes = {}

class ChangeHandler(FileSystemEventHandler):
    def on_modified(self, event):
        filepath = event.src_path
        filename = os.path.basename(filepath)

        if mimetypes.guess_type(filepath)[0] in ACCEPTED_FILE_TYPES:
            with open(filepath, 'rb') as f: # PermissionError occurs here
                b64_data = base64.b64encode(f.read())
                checksum = hashlib.md5(b64_data).hexdigest()

            if filename not in hashes or (hashes[filename] != checksum):
                hashes[filename] = checksum
                print('%s modified' % filename)
            else:
                pass


if __name__ == '__main__':
    event_handler = ChangeHandler()
    observer = Observer()
    observer.schedule(event_handler, './tmp')
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    finally:
        observer.join()
        print('shutting down...')
ta539tg70
  • 177
  • 1
  • 2
  • 17

0 Answers0