gpt4 book ai didi

python - 看门狗项目 : Reference before assignment error

转载 作者:行者123 更新时间:2023-11-28 16:56:49 25 4
gpt4 key购买 nike

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

我已经尝试在类中定义变量,甚至使用 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

最佳答案

当您的源代码被编译并且类被创建时,image_destination 变量还没有被初始化。上课前把作业移到最上面:

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

关于python - 看门狗项目 : Reference before assignment error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57654509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com