RarFile / [WinError 5]:访问被拒绝

时间:2017-06-21 20:35:16

标签: python windows extract zipfile rar

我试图编写一个脚本,它会自动从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,已解锁有问题的文件夹,并已打开相关文件夹和子文件夹的完全权限。有谁知道为什么会发生这种情况并且可能会出现问题?任何帮助非常感谢。

1 个答案:

答案 0 :(得分:1)

请参阅:Eryksun的评论

这不是安全许可问题。 UNRAR_TOOL应该是unrar程序的可执行文件名(可选择完整路径)。 subprocess.Popen失败,因为您正在尝试执行&#34; Python36-32&#34;目录。 - 昨天的eryksun

Windows API有一些相当无用的错误代码映射。在NT API的内部,在这种情况下的错误是STATUS_FILE_IS_A_DIRECTORY(0xC00000BA),这可能不是更明显,但它被Windows映射到ERROR_ACCESS_DENIED(0x0005),这误导你认为它是文件或对象的问题权限。 - 昨天的eryksun