首先,我尝试了只使用patool和patoolib。在出现错误后,我切换到了rarfile和unrar。第一个似乎更容易,但我不理解错误。第二个需要在环境变量中进行许多操作,我不确定我是否做对了。
import patoolib
patoolib.extract_archive("my_file.rar", outdir=r"C:\Users\User1\Desktop\Example_dir")
错误信息如下:
if verbosity >= 0:
TypeError: '>=' not supported between instances of 'str' and 'int'
我从这里得到了这个选项。我知道这个错误是关于字符串变量的,但我不知道如何解释它。
第二个选项是使用rarfile和unrar。
import patoolib
from unrar import rarfile
from pyunpack import Archive
rarfile.UNRAR_TOOL = r"C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll"
rarpath = 'my_file.rar'
rf = rarfile.RarFile(rarpath)
rf.extractall()
rf.extractall(r"C:\Users\User1\Desktop\Example_dir")
该选项会抛出一个恼人的错误:
PatoolError('patool can not unpack\n' + str(p.stderr)) pyunpack.PatoolError: patool can not unpack patool error: error extracting G:\program\test.rar: could not find an executable program to extract format rar; candidates are (rar,unrar,7z),
此外,还有另一个错误:
RarCannotExec: Unrar not installed? (rarfile.UNRAR_TOOL='unrar')
根据文档,rarfile要求UNRAR_TOOL是unrar.exe的路径。我已经使用"pip"安装了所有上述库,并按照此答案下载了UnRARDLL(http://www.rarlab.com/rar/UnRARDLL.exe),但我不知道应该将哪个.exe文件分配给UNRAR_TOOL。 我已将环境路径添加到C:\ Program Files(x86)\ UnrarDLL \ x64 \ UnRAR64.dll作为UNRAR_LIB_PATH,但未能解决问题。
我只想通过Python脚本解压缩一些文件。越简单越好。您能告诉我我做错了什么吗?也许还有其他解压文件的方法?
SyntaxError: EOL while scanning string literal
是因为你在program=r"
后忘记了一个闭合的引号"
,在你的闭合括号)
之前。 - nyov