【发布时间】:2021-06-22 19:05:06
【问题描述】:
我还是个编程新手,我决定编写一个脚本,让我个人受益,同时测试我的能力。我创建了一个脚本,它使用看门狗来跟踪我机器上的下载文件夹。我想让它尽可能地动态化,这样理论上我可以将程序提供给朋友,他们可以将所需的路径输入到随附的 .txt 文件中,并且它将继续为他们无缝工作。从某种意义上说,我的程序正在运行;如果我将图片文件从一个文件夹拖到下载文件夹中,它会自动将其发送到图片文件夹。我的问题出现在新的下载中。每当我在脚本运行时尝试在 chrome 上下载图片时,我都会收到“失败 - 下载错误”。几个小时以来,我一直在试图弄清楚这是什么,最后决定尝试寻求一些建议。
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os
import time
from shutil import move
paths_dict = {'dl_path': [], 'img_path': [], 'audio_path': [], 'vid_path': [],
'doc_path': [], 'exe_path': [], 'zip_path': [], 'other_path': []}
keys = list(paths_dict.keys())
val_list = []
p_len = len(paths_dict.keys())
with open('config.txt', 'r') as paths:
for line in paths.readlines():
line.strip()
split = list(line.split())
path = split[-1]
val_list.append(path)
while p_len != 0:
for k in keys:
paths_dict[k].append(val_list[0])
val_list.remove(val_list[0])
p_len -= 1
image_formats = ["jpg", "png", "jpeg", "gif", "webp", "tiff", "bmp",
"heif", "jfif", "bat", "webp", "tiff"]
audio_formats = ["mp3", "wav", "ogg", "flac", "m4a", "aac"]
video_formats = ["mp4", "avi", "webm", "mov", 'mkv']
docs_formats = ["ai", "ait", "rtf", "txt", "doc", "docx", "pdf"]
executables = ["exe"]
zip_folders = ["zip", "tar", "gz", "7z"]
class MyHandler(FileSystemEventHandler):
def on_any_event(self, event):
for file in os.listdir(paths_dict['dl_path'][0]):
extension = (file.split('.')[-1].lower())
src = paths_dict['dl_path'][0] + '\\' + file
if extension in image_formats:
folder_dst = paths_dict['img_path'][0]
new_dst = folder_dst + '\\' + file
move(src, new_dst)
elif extension in video_formats:
folder_dst = paths_dict['vid_path'][0]
new_dst = folder_dst + '\\' + file
move(src, new_dst)
elif extension in docs_formats:
folder_dst = paths_dict['doc_path'][0]
new_dst = folder_dst + '\\' + file
move(src, new_dst)
elif extension in executables:
folder_dst = paths_dict['exe_path'][0]
new_dst = folder_dst + '\\' + file
move(src, new_dst)
elif extension in zip_folders:
folder_dst = paths_dict['zip_path'][0]
new_dst = folder_dst + '\\' + file
move(src, new_dst)
else:
folder_dst = paths_dict['other_path'][0]
new_dst = folder_dst + '\\' + file
move(src, new_dst)
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, paths_dict['dl_path'][0], recursive=False)
observer.start()
try:
while True:
time.sleep(5)
except KeyboardInterrupt:
observer.stop()
observer.join()
这是我的 .txt 文件中包含的内容:
Path to downloads folder: C:\Users\12483\Downloads
Path of where downloaded image files are to be stored: C:\Users\12483\Pictures
Path of where downloaded audio files are to be stored: C:\Users\12483\Music
Path of where downloaded video files are to be stored: C:\Users\12483\Videos
Path of where downloaded document files are to be stored: C:\Users\12483\Documents
Path of where downloaded executable files are to be stored: C:\Users\12483\exe_files
Path of where downloaded zip files are to be stored: C:\Users\12483\zip_files
Path of where all other file types are to be stored: C:\Users\12483\misc_files
【问题讨论】:
标签: python automation scripting