【发布时间】:2017-11-24 22:21:20
【问题描述】:
我正在尝试编写一个脚本,它会自动从 rar 或 zip 文件夹中提取文件并将它们放在某个位置,以便更快地组织文件。包括相关的代码部分:
import shutil
import os
import eyed3
import glob
import zipfile
import rarfile
import unrar
import patoolib
## create zipfile object of the downloaded album and get a tracklist
rarfile.UNRAR_TOOL=r'C:\Users\John\AppData\Local\Programs\Python\Python36-32'
downloads = glob.glob("C:\\Users\\John\\Downloads\\*")
music_zip = max(downloads, key=os.path.getctime)
if os.path.splitext(music_zip)[-1] == '.zip':
music_folder = zipfile.ZipFile(music_zip)
elif os.path.splitext(music_zip)[-1] == '.rar':
music_folder = rarfile.RarFile(music_zip)
print(music_zip)
print(music_folder)
temporary_album_folder = 'C:\\Users\\John\\Downloads\\temporary_album_folder'
if not os.path.exists(temporary_album_folder):
os.makedirs(temporary_album_folder)
# patoolib.extract_archive(music_zip, outdir=temporary_album_folder)
# temp_list = os.listdir(temporary_album_folder)
# tag = eyeD3.load(temp_list[0])
# artist = tag.getArtist()
# album = tag.getAlbum()
# print(os.getcwd())
os.chdir(temporary_album_folder)
music_folder.extractall()
music_folder.close()
print(temporary_album_folder)
当我运行它时,我希望它能够成功地将 RAR 的内容提取到 \Downloads 中的一个临时文件夹中。相反,当我尝试在控制台中运行它时收到的错误消息是:
C:\Users\John\Documents\PythonScripts>music_organizer.py
C:\Users\John\Downloads\d1ctus t3 n3c4r3(5).rar
<rarfile.RarFile object at 0x02C16350>
Traceback (most recent call last):
File "C:\Users\John\Documents\PythonScripts\music_organizer.py", line 40, in <
module>
music_folder.extractall()
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\site-package
s\rarfile.py", line 820, in extractall
self._extract(fnlist, path, pwd)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\site-package
s\rarfile.py", line 885, in _extract
p = custom_popen(cmd)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\site-package
s\rarfile.py", line 2813, in custom_popen
creationflags=creationflags)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\subprocess.p
y", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\subprocess.p
y", line 990, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied
我知道很多其他人都问过关于 WinError 5 和 Python 的类似问题,所以提前解决可能的常见建议:我正在以管理员模式运行终端,已关闭 UAC,已解锁相关文件夹,并已打开相关文件夹和子文件夹的完全权限。有谁知道为什么会发生这种情况以及可能的解决方法?非常感谢任何帮助。
【问题讨论】:
-
不是安全权限问题。
UNRAR_TOOL应该是解压缩程序的可执行名称(可选完整路径)。subprocess.Popen失败,因为您正在尝试执行“Python36-32”目录。 -
我是个白痴!非常感谢!
-
Windows API 有一些相当无用的错误代码映射。在 NT API 内部,这种情况下的错误是
STATUS_FILE_IS_A_DIRECTORY(0xC00000BA),这再明显不过了,但它被 Windows 映射到ERROR_ACCESS_DENIED(0x0005),这会误导您认为这是文件或文件的问题对象权限。 -
@eryksun 将所有信息添加到答案中。
标签: python windows extract zipfile rar