Streamlit上传并解压RAR文件
测试程序如下:
import streamlit as st
import os
import rarfile
def unrar_file(rar_file, output_dir):
with rarfile.RarFile(rar_file, 'r') as rar_ref:
rar_ref.extractall(output_dir)
uploaded_files = st.file_uploader(":rainbow[**请上传文件压缩包**]", type=['zip','rar'], accept_multiple_files=True)
for uploaded_file in uploaded_files:
archive_file = os.path.join('./', uploaded_file.name)
with open(archive_file, 'wb') as f:
f.write(uploaded_file.getvalue())
unrar_file('./'+uploaded_file.name, './unrar_output')
报错:rarfile.BadRarFile: Failed the read enough data: req=1048576 got=5
上传RAR文件,解压时报错:
web界面报错如图所示:
解决方案
下载UnRAR.exe: UnRAR
将UnRAR.exe放到Python环境中的Scripts文件夹中,如图所示:
重新运行app.py
再次测试,RAR文件解压成功
希望本文对大家有帮助,上文若有不妥之处,欢迎指正
分享决定高度,学习拉开差距