首页 > 解决方案 > 打开目录时出现 PermissionError

问题描述

我正在设置一个 python 脚本来组织我的下载文件夹,但是每次下载文件时,或者当我打开下载文件夹中的文件夹时,我都会收到以下错误:

Traceback (most recent call last):
  File "D:\Apps\Anaconda\lib\shutil.py", line 788, in move
    os.rename(src, real_dst)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'D:\\Downloads\\cd4d05fb-407e-4d59-996b-26d86a20a44c.tmp' -> 'D:\\Downloads\\tmp\\cd4d05fb-407e-4d59-996b-26d86a20a44c.tmp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Documents/Shortcuts/Folder Organizer.pyw", line 29, in <module>
    shutil.move("D:\Downloads\{}".format(arquivo), "D:\Downloads\{}".format(ext))
  File "D:\Apps\Anaconda\lib\shutil.py", line 803, in move
    os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'D:\\Downloads\\cd4d05fb-407e-4d59-996b-26d86a20a44c.tmp'

这是我的代码ATM:

import os
import shutil
from mutagen.easyid3 import EasyID3
while True:
    directory = os.listdir("D:\Downloads")
    for arquivo in directory:
        directory = os.listdir("D:\Downloads")
        filename = arquivo.split(".")
        ext = filename[-1]
        filename = arquivo.split("-")
        if filename[0] == "MEGA":
            year = filename[3].split(".")
            year = year[0]
            shutil.move("D:\Downloads\{}".format(arquivo), "D:\Music\MEGA")
            audiofile = EasyID3("D:\Music\MEGA\{}".format(arquivo))
            audiofile["title"] = u"{}".format(filename[1])
            audiofile["album"] = u"MEGA"
            audiofile["artist"] = u"{}".format(filename[2])
            if filename[-1] != filename[3]:
                audiofile["albumartist"] = u"{}".format(filename[4])
            audiofile.save()
        elif ext == "mp3":
            shutil.move("D:\Downloads\{}".format(arquivo), "D:\Music")
        elif ext == "mp4":
            shutil.move("D:\Downloads\{}".format(arquivo), "D:\Videos")
        else:
            if ext not in directory:
                os.mkdir("D:\Downloads\{}".format(ext))
            shutil.move("D:\Downloads\{}".format(arquivo), "D:\Downloads\{}".format(ext))

开始的时候是因为我想把它变成一个在 Windows 后台运行的进程,我认为这是每次将新文件放入下载文件夹时让它运行的方法

我知道那里有一些程序可以自动组织这样的文件夹,但我想在 Python 中设置它作为对自己的挑战,有点卡在这些错误中。

编辑:稍微更改了代码以处理异常,还添加了一个计时器,因此脚本不会像以前那样运行多次。还添加了来自网站的一段代码:http: //timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html 在“使用 os.listdir 轮询目录”下。它现在按照我的意图运行。代码如下:

import os
import shutil
from mutagen.easyid3 import EasyID3
import time

directory = "D:/Downloads"
before = dict ([(f, None) for f in os.listdir (directory)])
while True:
    time.sleep (1)
    after = dict ([(f, None) for f in os.listdir (directory)])
    added = [f for f in after if not f in before]
    before = after
    print("added: {}".format(added))
    try:
        if added:
            filelist = os.listdir("D:/Downloads")
            for item in filelist:
                print("entrou")
                allfiles = os.listdir("D:\Downloads")
                filename = item.split(".")
                ext = filename[-1]
                filename = item.split("-")
                if filename[0] == "MEGA":
                    year = filename[3].split(".")
                    year = year[0]
                    shutil.move("D:\Downloads\{}".format(item), "D:\Music\MEGA")
                    audiofile = EasyID3("D:\Music\MEGA\{}".format(item))
                    audiofile["title"] = u"{}".format(filename[1])
                    audiofile["album"] = u"MEGA"
                    audiofile["artist"] = u"{}".format(filename[2])
                    if filename[-1] != filename[3]:
                        audiofile["albumartist"] = u"{}".format(filename[4])
                    audiofile.save()
                elif ext == "mp3":
                    try:
                        shutil.move("D:\Downloads\{}".format(item), "D:\Music")
                    except:
                        none
                elif ext == "mp4":
                    try:
                        shutil.move("D:\Downloads\{}".format(item), "D:\Videos")
                    except:
                        none
                else:
                    if ext not in allfiles:
                        try:
                            os.mkdir("D:\Downloads\{}".format(ext))
                        except:
                            none
                    try:
                        shutil.move("D:\Downloads\{}".format(item), "D:\Downloads\{}".format(ext))
                    except:
                        none
    except:
        none

标签: python

解决方案


推荐阅读