Python 看门狗忽略模式

Python 看门狗忽略模式,python,python-3.x,watchdog,python-watchdog,ignore-files,Python,Python 3.x,Watchdog,Python Watchdog,Ignore Files,我试着和这里的其他帖子一起讨论这个问题,但没能成功。我是Python的新手 我需要有关忽略模式的帮助 我正在将图像上载到一个文件夹中,并且正在使用\uuu临时添加图像,因此在上载文件时添加的实际图像是\uu image-name.jpg。上传完成后,它会再次添加为image-name.jpg(并删除\uu image-name.jpg) 我想用watchdog忽略所有\uu image-name.jpg文件 这是我的密码: class Watcher: DIRECTORY_TO_WATC

我试着和这里的其他帖子一起讨论这个问题,但没能成功。我是Python的新手

我需要有关
忽略模式的帮助

我正在将图像上载到一个文件夹中,并且正在使用
\uuu
临时添加图像,因此在上载文件时添加的实际图像是
\uu image-name.jpg
。上传完成后,它会再次添加为
image-name.jpg
(并删除
\uu image-name.jpg

我想用watchdog忽略所有
\uu image-name.jpg
文件

这是我的密码:

class Watcher:
    DIRECTORY_TO_WATCH = "director/where/images/are/uploaded"

    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(5)
        except:
            self.observer.stop()
            print("Error")

        self.observer.join()


class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None

        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print(event.src_path)
            img = Image.open(event.src_path)
            for result in engine.classify_with_image(img, top_k=3):
              print('---------------------------')
              print(labels[result[0]])
              print('Score : ', result[1])

        # elif event.event_type == 'modified':
            # Taken any action here when a file is modified.
            # print("Received modified event - %s." % event.src_path)

        elif event.event_type == 'deleted':
            # Taken any action here when a file is deleted.
            print("Received deleted event - %s." % event.src_path)

if __name__ == '__main__':
    w = Watcher()
    w.run()

非常感谢。

是否
事件。src_path
返回字符串?如果是,您可以使用string类的
startswith
方法跳过不需要的图像。 例如:

elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print(event.src_path)
            
            # Check if this filename starts with "__" and execute the next block
            if not event.src_path.startswith('__'):
              img = Image.open(event.src_path)
              for result in engine.classify_with_image(img, top_k=3):
                print('---------------------------')
                print(labels[result[0]])
                print('Score : ', result[1])
            # else do nothing

谢谢!它返回图像的完整路径:home/pi/Media/plant classification/1200px-Sunflower\u sky\u background.jpg一些带有
的内容。包含
的内容,也许?如果“\uuuux”可以使用此
不是在事件中。src_path:
非常感谢您的帮助。我对Python和使用Coral Accelerator非常熟悉。仍在努力解决问题。没问题,对答案的投票将是晚餐:)