简体   繁体   English

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

[英]RarFile / [WinError 5]: Access Denied

I'm trying to write a script that will automatically extract the files from a rar or zip folder and put them somewhere, so as to make file organization faster.我正在尝试编写一个脚本,该脚本会自动从 rar 或 zip 文件夹中提取文件并将它们放在某处,以便更快地组织文件。 Included are the relevant sections of code:包括代码的相关部分:

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)

When I run this, I expect it to successfully extract the contents of the RAR into a temporary folder in \\Downloads.当我运行它时,我希望它能够成功地将 RAR 的内容提取到 \\Downloads 中的一个临时文件夹中。 Instead, the error message that I get when I try to run this in the console is:相反,当我尝试在控制台中运行它时得到的错误消息是:

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

I know a lot of other people have asked similar questions about WinError 5 and Python, so to address possible common suggestions in advance: I am running the terminal in admin mode, have turned off UAC, have unblocked the folder in question, and have opened full permissions to the folder and sub-folders in question.我知道很多其他人都问过有关 WinError 5 和 Python 的类似问题,因此为了提前解决可能的常见建议:我正在以管理员模式运行终端,已关闭 UAC,已取消阻止相关文件夹,并已打开有问题的文件夹和子文件夹的完全权限。 Does anyone know why this is happening and possible get arounds?有谁知道为什么会发生这种情况并可能绕过? Any help much appreciated.非常感谢任何帮助。

Refer to: Eryksun's comment参考:Eryksun的评论

It's not a security permission issue.这不是安全权限问题。 UNRAR_TOOL should be the executable name (optionally the full path) of an unrar program. UNRAR_TOOL 应该是 unrar 程序的可执行文件名(也可以是完整路径)。 subprocess.Popen is failing because you're trying to execute the "Python36-32" directory. subprocess.Popen 失败,因为您正在尝试执行“Python36-32”目录。 – eryksun yesterday – eryksun 昨天

The Windows API has some rather useless error code mappings. Windows API 有一些相当无用的错误代码映射。 Internally in the NT API the error in this case is STATUS_FILE_IS_A_DIRECTORY (0xC00000BA), which could not be more obvious, but it gets mapped to ERROR_ACCESS_DENIED (0x0005) by Windows, which misleads you into thinking it's a problem with file or object permissions.在 NT API 内部,这种情况下的错误是 STATUS_FILE_IS_A_DIRECTORY (0xC00000BA),这再明显不过了,但它会被 Windows 映射到 ERROR_ACCESS_DENIED (0x0005),这会误导您认为这是文件或对象权限的问题。 – eryksun yesterday – eryksun 昨天

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM