【问题标题】:Automated organizer for downloads folder causing all chrome downloads to fail下载文件夹的自动管理器导致所有 chrome 下载失败
【发布时间】: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


    【解决方案1】:

    这其实是个很酷的主意……

    作为一个没有太多使用 python 进行文件管理经验的人。我认为我无法直接胜任地修改您的代码,但我有一个理论。

    当浏览器开始下载文件时,它首先创建一个模板,该模板只是文件的一部分,称为 .part 文件。

    https://fileinfo.com/extension/part#:~:text=A%20PART%20file%20is%20a,program%20that%20started%20the%20download.

    我假设您的 python 脚本正在尝试在下载完成之前组织此文件。

    所以解决方案(理论上)是修改你的脚本以等待 .part 文件成为正确的类型,也许是通过忽略带有 .part 的文件

    编辑:

    我需要橡皮鸭提出另一种我们想要忽略的部分文件。(.crdownload) 很容易有更多。

    这是一种可能的解决方案:

    声明

    partial_files = ["crdownload", "part"]
    

    然后在你的 for 循环中:

                elif extension in zip_folders:
                    folder_dst = paths_dict['zip_path'][0]
                    new_dst = folder_dst + '\\' + file
                    move(src, new_dst)
    
                # new code
                elif extension in partial_files:
                     pass # this essentially means do nothing
                # /new code
                else:
                    folder_dst = paths_dict['other_path'][0]
                    new_dst = folder_dst + '\\' + file
                    move(src, new_dst)
    

    【讨论】:

    • 之前我发现 Chrome、Edge 等(基于铬)会生成一个“.crdownload”。所以我尝试实现一个while循环来等待它下载。类似于在循环之前设置 wait = True 的内容,然后基于此启动一个 while 循环。基本上只要 .crdownload 在 Downloads 文件夹中,它就会保持 True,一旦完成它就会变为 False,理论上会打破循环。我从代码中删除了它,因为它似乎没有任何效果。作为问题的答案,这确实是有道理的,我对如何在上下文中实现它没有太多的想法。
    • while 循环暂停是不好的做法。但这来自对 Python 中的线程管理不太了解的人,所以我真的不知道为什么该解决方案不起作用。我用另一个想法更新了我的答案......祝你好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 2018-06-10
    相关资源
    最近更新 更多