簡體   English   中英

Python 提取/解壓 RAR 文件錯誤

[英]Python extract / unrar RAR file errors

我正在嘗試在 Python 腳本中提取 RAR 文件。 我發現只有兩種可能的方法來做到這一點:使用 patolib 或使用rarfile 不幸的是,這兩個選項都會在我的代碼中引發很多錯誤,我不知道如何解決這個問題。

首先,我只嘗試了 patool 和 patolib。 出現錯誤后,我切換到 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 install unrar”,我已經通過“pip”安裝了上面的所有庫。 基於這個答案,我已經下載了 UnRARDLL ( http://www.rarlab.com/rar/UnRARDLL.exe ),但我不知道我應該將什么 .exe 文件分配給 UNRAR_TOOL。 我已將環境路徑添加到 C:\\Program Files (x86)\\UnrarDLL\\x64\\UnRAR64.dll 作為 UNRAR_LIB_PATH 但它沒有幫助。

我只想通過 Python 腳本解壓縮一些文件。 越容易越好。 你能告訴我我做錯了什么嗎? 也許有另一種方法來解壓縮某些文件?

TypeError異常聲稱您試圖比較字符串和整數。 如果對if verbosity >= 0:的引用是正確的,則意味着verbosity變量是一個字符串。
也許您之前設置了verbosity = '1'而不是verbosity = 1

另一個錯誤就是它所說的: could not find an executable program to extract format rar; candidates are (rar,unrar,7z) could not find an executable program to extract format rar; candidates are (rar,unrar,7z)
該代碼期望找到rarunrar7z (7zip) 之一的可執行文件。 如果您安裝了它們,也許您需要以某種方式告訴patoolib有關它們的信息。

rarfile.UNRAR_TOOL = r"C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll"

看起來不錯,但如果它不起作用,您可能必須按照鏈接答案中的步驟操作並設置UNRAR_LIB_PATH環境變量。
這應該解釋如何在 Windows 上設置環境變量: https : //helpdeskgeek.com/windows-10/add-windows-path-environment-variable/

不久前,我找到了 patolib 的主 現在我知道, patoolib.extract_archive函數的參數是: patoolib.extract_archive(archive, verbosity, outdir, program, format, compression) 我不知道verbosity在這個函數中做了什么。 另外,我應該如何設置program 現在我有這樣的事情:

import patoolib
patoolib.extract_archive(archive="my_file.rar", verbosity=0 ,outdir=or"C:\Users\User1\Desktop\Example_dir", program=r"C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll)

錯誤說:

SyntaxError: EOL while scanning string literal

您需要到https://www.rarlab.com/rar_add.htm下載 UnRAR for Windows - 命令行免費軟件 Windows UnRAR,將其解壓到一個文件夾並添加:

rarfile.UNRAR_TOOL = r"C:\YourPath\UnRAR.exe"

因為 rarfile 不是在尋找 .dll,而是在尋找可執行文件 .exe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM