python - 打开目录时出现 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
解决方案
推荐阅读
- javascript - window.confirm - 取消按钮上的空格与回车键
- laravel - 在 VueJS 道具中传递 Laravel 控制器功能
- javascript - Firefox 中 Chrome 覆盖范围的替代方案
- javascript - 如何修复 Uncaught ReferenceError: require is not defined - 如何修复它?
- javascript - FlowType 中的自定义解析器
- java - Java:类的客户端服务器反向功能
- c# - HashCode.Combine 是可交换的吗?
- ssl-certificate - 访问 VisualEditor for MediaWiki 时出现 SSL 错误
- php - 如何使 PHP 变量可以在不同的文件中访问?
- c++ - 将模板类型与默认类型一起使用,不带尖括号