rarfile.BadRarFile: Failed the read enough data: req=1048576 got=5
时间: 2024-04-12 14:28:25 浏览: 33
这个错误通常是由于RAR文件损坏或格式不正确引起的。你可以尝试使用`try-except`语句来捕获该错误,并继续处理其他RAR文件。以下是修改后的示例代码:
```python
import os
import rarfile
# 获取当前文件夹路径
current_dir = os.getcwd()
# 遍历当前文件夹中的所有文件
for file_name in os.listdir(current_dir):
if file_name.endswith('.rar'): # 判断文件是否以.rar结尾
file_path = os.path.join(current_dir, file_name) # 拼接文件路径
try:
# 解压RAR文件
with rarfile.RarFile(file_path) as rf:
rf.extractall(current_dir)
except rarfile.BadRarFile:
print(f"无法解压文件: {file_name},可能文件损坏或格式不正确。")
```
通过添加`try-except`语句,当遇到`rarfile.BadRarFile`错误时,代码将打印一条错误消息并继续处理下一个RAR文件。
如果你仍然遇到问题,可能需要检查RAR文件的完整性或尝试使用其他解压缩工具来处理这些文件。希望对你有所帮助!如有其他问题,请继续提问。
相关问题
linux 用python 定时任务解压 rar报错 rarfile.RarCannotExec: Cannot find working tool 怎么解决
这个错误是因为rarfile模块在解压rar文件时需要调用rar命令,但是系统中没有安装rar命令或者rar命令没有加入到环境变量中,导致无法执行rar命令。
解决方法是安装rar命令并加入环境变量中,具体步骤如下:
1. 安装rar命令
可以使用以下命令安装rar命令:
```
sudo apt-get install rar
```
2. 查看rar命令的安装路径
安装完成后,可以使用以下命令查看rar命令的安装路径:
```
which rar
```
一般情况下,rar命令的安装路径为 /usr/bin/rar。
3. 将rar命令的安装路径加入环境变量
可以修改当前用户的环境变量文件,将rar命令的安装路径加入PATH环境变量中。
例如,在bash环境下,可以使用以下命令修改环境变量文件:
```
echo 'export PATH=$PATH:/usr/bin/rar' >> ~/.bashrc
source ~/.bashrc
```
修改完成后,重新运行程序即可成功执行rar命令解压rar文件。
另外,如果运行程序的用户不是root用户,则需要使用sudo命令来安装rar命令和修改环境变量文件。
import string import itertools from Crypto.Cipher import ARC4 import rarfile import concurrent.futures def gen_password(passwd): key = passwd.encode() cipher = ARC4.new(key) return cipher rar_path = "E:/Edge浏览器下载/30.rar" rar = rarfile.RarFile(rar_path) def try_passwords(rar_path, dictionary, max_workers=4): rar = rarfile.RarFile(rar_path) tested = set() with concurrent.futures.ProcessPoolExecutor(max_workers) as executor: fs = [] for password in dictionary: password_str = ''.join(password) if password_str in tested: continue tested.add(password_str) decrypt_pwd = gen_password(password_str) fs.append(executor.submit(rar.extractall, pwd=decrypt_pwd)) for index, future in enumerate(concurrent.futures.as_completed(fs)): try: future.result() print(f'成功!密码是: {dictionary[index]}') return dictionary[index] except (rarfile.BadRarFile, rarfile.RarWrongPassword): pass characters = string.ascii_letters + string.digits + string.punctuation dictionary = itertools.product(characters, repeat=1) result = try_passwords(rar_path, dictionary) print(result) 更改该代码的错误
这段代码缺少一些必要的库和变量定义,我假设你想使用一个密码字典文件来进行暴力破解。以下是更改后的代码:
```
import string
import itertools
import rarfile
import concurrent.futures
from Crypto.Cipher import ARC4
def gen_password(passwd):
key = passwd.encode()
cipher = ARC4.new(key)
return cipher
rar_path = "E:/Edge浏览器下载/30.rar"
def try_passwords(rar_path, dictionary_file, max_workers=4):
rar = rarfile.RarFile(rar_path)
tested = set()
with open(dictionary_file, 'r') as f:
dictionary = f.read().splitlines()
with concurrent.futures.ProcessPoolExecutor(max_workers) as executor:
fs = []
for password in dictionary:
password_str = password.strip()
if password_str in tested:
continue
tested.add(password_str)
decrypt_pwd = gen_password(password_str)
fs.append(executor.submit(rar.extractall, pwd=decrypt_pwd))
for index, future in enumerate(concurrent.futures.as_completed(fs)):
try:
future.result()
print(f'成功!密码是: {dictionary[index]}')
return dictionary[index]
except (rarfile.BadRarFile, rarfile.RarWrongPassword):
pass
result = try_passwords(rar_path, 'passwords.txt')
print(result)
```
你需要将 `passwords.txt` 替换为你自己的密码字典文件路径。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)