看门狗项目:赋值错误前的引用

Watchdog Project: Reference before assignment error

我正在尝试创建一个程序,将下载的文件分配到我选择的特定目录,但是这似乎只适用于一个 if 语句,每当我将它扩展到多个时,它只会给我错误如下所示。

我试过在 class 中甚至使用 for 循环定义变量,但似乎没有任何效果。

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os
import json
import time

class MyHandler(FileSystemEventHandler):  
    def on_modified(self, event):
        for filename in os.listdir(folder_to_track):    
            if filename.endswith(('.jpeg', '.jpeg', '.png', '.tiff', 
               '.gif', '.raw', '.psd')):

                src1 = folder_to_track.join(filename)
                image_destination = image_destination + '/' + filename
                os.rename(src1, image_destination)

            elif filename.endswith(('.txt', '.tex', '.pdf', '.doc', 
                '.docx', '.rtf', '.odt', '.wks')):

                src2 = folder_to_track.join(filename)
                text_destination = text_destination + '/' + filename
                os.rename(src2, text_destination)

            elif filename.endswith(('.mp4', '.ogg', '.wmv', '.flv', 
                 '.wav', '.avi', '.h.26*', '.vp*', '.mpeg*')):

                src3 = folder_to_track.join(filename)
                video_destination = video_destination + '/' + filename
                os.rename(src3, video_destination)
            else:
                print('Unrecognize file.')


folder_to_track = "/home/$USER/Downloads/"
image_destination = "/home/$USER/Desktop/Images"
video_destination = "/home/$USER/Desktop/Video"
text_destination = "/home/$USER/Desktop/Text"

event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, folder_to_track, recursive=True)
observer.start()

try:
    while True:
        time.sleep(5)
except KeyboardInterrupt:
    observer.stop()
observer.join()

应该发生什么: [文件刚刚移过来,没有任何消息]

当前情况:

(我认为我们可以忽略导入的库错误,因为当我只使用一个 if 语句时这些错误似乎有效,但我可能是错的)

线程 Thread-1 中的异常:

Traceback (most recent call last):
   File "/usr/lib/python2.7/threading.py", line 801, in 
    __bootstrap_inner
    self.run()
  File "/home/luciano/.local/lib/python2.7/site- 
    packages/watchdog/observers/api.py", line 199, in run
    self.dispatch_events(self.event_queue, self.timeout)
  File "/home/luciano/.local/lib/python2.7/site- 
    packages/watchdog/observers/api.py", line 368, in 
    dispatch_events
    handler.dispatch(event)
  File "/home/luciano/.local/lib/python2.7/site- 
     packages/watchdog/events.py", line 330, in dispatch
    _method_map[event_type](event)

相关错误:

  File "pipe.py", line 13, in on_modified
    image_destination = image_destination + '/' + filename
    UnboundLocalError: local variable 'image_destination' 
    referenced before assignment

编译源代码并创建 class 时,image_destination 变量尚未初始化。 将作业移到顶部 class:

之前
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os
import json
import time


class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        folder_to_track = "/home/$USER/Downloads/"
        image_destination = "/home/$USER/Desktop/Images"
        video_destination = "/home/$USER/Desktop/Video"
        text_destination = "/home/$USER/Desktop/Text"
        for filename in os.listdir(folder_to_track):
            if filename.endswith(('.jpeg', '.jpeg', '.png', '.tiff',
               '.gif', '.raw', '.psd')):

                src1 = folder_to_track.join(filename)
                image_destination = image_destination + '/' + filename
                os.rename(src1, image_destination)

            elif filename.endswith(('.txt', '.tex', '.pdf', '.doc',
                '.docx', '.rtf', '.odt', '.wks')):

                src2 = folder_to_track.join(filename)
                text_destination = text_destination + '/' + filename
                os.rename(src2, text_destination)

            elif filename.endswith(('.mp4', '.ogg', '.wmv', '.flv',
                 '.wav', '.avi', '.h.26*', '.vp*', '.mpeg*')):

                src3 = folder_to_track.join(filename)
                video_destination = video_destination + '/' + filename
                os.rename(src3, video_destination)
            else:
                print('Unrecognize file.')



folder_to_track = "/home/$USER/Downloads/"
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, folder_to_track, recursive=True)
observer.start()

try:
    while True:
        time.sleep(5)
except KeyboardInterrupt:
    observer.stop()
observer.join()