我正在编写一个 python 脚本来处理一些包含许多 .rar 文件的旧档案。我可以使用 pyunpack 中的 Archive.extractall 成功地从中提取数据,但无法找到太多文档。 https://pypi.org/project/pyunpack/0.1.2/ 上的文档链接已损坏。一切都很顺利,直到程序挂在特定的 .rar 文件上。我用 7zip 查看了该文件,它说它受密码保护。我不知道密码是什么,所以我希望脚本简单地跳过它。我已经在 try 块中包含了 extractall ,但我想也不会有例外。如何测试文件是否受密码保护,如果是,则不尝试提取它?这是我的代码的相关部分。
import os
import rarfile
from pyunpack import Archive
for subdir,dirs,files in os.walk(directoryToExtract):
for file in files:
if rarfile.is_rarfile(os.path.join(subdir, file)):
print("Found rarfile " + os.path.join(subdir, file))
try:
Archive(os.path.join(subdir, file)).extractall(os.path.join(subdir, file[0:len(file)-4]),auto_create_dir=True)
except Exception as e:
print("ERROR: Couldn't unrar " + os.path.join(subdir, file))
print(str(e))
这适用于提取受密码保护的 RAR 文件。 尝试: patoolib.extract_archive(rar_file, outdir=extract_dir, 密码=“密码”) 除了: 继续