Cybergenik
Cybergenik

Reputation: 140

Watchdog Project: Reference before assignment error

I'm trying to create a program that allocates downloaded files to a specific directory of my choosing, however this seems to only work with one if statement, whenever I expanded it to more than one, it just gives me the error shown below.

I've tried defining variables within the class and even withing the for loop, but nothing seems to work.

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

Whats supposed to happen: [file just moved over, no messages nothing]

Whats currently happening:

(I think we can disregard the imported library errors, since those seemed to work when I only used one if statement, but I might be wrong)

Exception in thread 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)

Related error:

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

Upvotes: 3

Views: 276

Answers (1)

Kostas Charitidis
Kostas Charitidis

Reputation: 3103

When your source is compiled and the class gets to be created the image_destination variable is not yet initialized. Move the assignments on top before the 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()

Upvotes: 2

Related Questions