Python rarfile.BadRarFile: Failed the read enough data got=0解决

1,451 阅读1分钟

在使用rarfile批量解压有密码rar的时候遇到报错:
raise BadRarFile("Failed the read enough data: req=%d got=%d" % (orig, len(data)))
rarfile.BadRarFile: Failed the read enough data: req=1048576 got=0

在安装UnrarDLL和设置环境变量UNRAR_LIB_PATH:C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll之后,再将UnRAR.exe放在Python根目录,但并没有解决问题

最终解决方案竟是将import rarfile修改为from unrar import rarfile,难绷

尝试多个密码解压rar,代码部分如下:

from unrar import rarfile
if ext == ".rar":
    for password in passwords:
        try:
            with rarfile.RarFile(file, mode='r') as f:
                f.extractall(path_out,None,password)  # 解压文件名、解压后路径、密码
                return True # 如果成功解压,就返回True
        except Exception as e:
            traceback.print_exc()
            print(f"{file}:{e}")
            continue # 如果密码错误,就继续尝试下一个密码