为什么网站建设要值班,网络课程营销推广方案,自助建网站,怎么注销自己名下的公司一、7z压缩文件的压缩和解压
1、安装py7zr
我们要先安装py7zr第三方库#xff1a;
pip install py7zr如果python环境有问题#xff0c;执行上面那一条安装语句老是安装在默认的python环境的话#xff0c;我们可以执行下面这条语句#xff0c;将第三方库安装在项目的虚拟…一、7z压缩文件的压缩和解压
1、安装py7zr
我们要先安装py7zr第三方库
pip install py7zr如果python环境有问题执行上面那一条安装语句老是安装在默认的python环境的话我们可以执行下面这条语句将第三方库安装在项目的虚拟环境中
pip install py7zr --targetE:\Python脚本\作业查重\OS_Study\venv\Lib\site-packages2、解压7z文件
import py7zr
# 将压缩文件解压到指定目录
def decompress_7z():# 将要解压的压缩文件路径archive py7zr.SevenZipFile(rE:\Python脚本\作业查重\20大数据班Javaweb新闻系统.7z, moder)# 压缩文件的解压目录archive.extractall(pathrE:\Python脚本\作业查重\20大数据班Javaweb新闻系统)archive.close()3、压缩成7z文件
import py7zr
# 将指定目录压缩到指定压缩文件test.7z
def compression_7z():# 生成的压缩文件路径archive py7zr.SevenZipFile(rE:\Python脚本\作业查重\test.7z, modew)# 需要压缩的压缩文件archive.writeall(pathr../test)archive.close()二、rar压缩文件的压缩和解压
1、环境准备
我们用到的第三方库为rarfile因为我们的这个第三方库需要用到第三方程序所以我们要先配一下环境。
1导入unrar模块
pip install unrar2下载 unrar library 并按照默认安装路径安装下载链接下载
3 编辑环境变量 用户变量 - 变量名x64 - 变量值C:\Program Files (x86)\UnrarDLL\x64 默认路径是这个 系统变量 - 变量名UNRAR_LIB_PATH - 变量值C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll 默认路径[32位系统下的变量值为C:\Program Files (x86)\UnrarDLL\UnRAR.dll] 4安装winrar360软件中心有 将 winrar 的目录下的 unrar.exe 复制到 Python 路径的 Scripts 文件夹下。 5重启Pycharm
2、安装rarfile
执行以下命令
pip install rarfile3、解压rar文件
import rarfile
def decompress_rar():# 找到rar文件z rarfile.RarFile(rE:\Python脚本\作业查重\2015090103石凯-新闻管理系统.rar) # 指定解压输出的目录z.extractall(rE:\Python脚本\作业查重\2015090103石凯-新闻管理系统) z.close()# 删除压缩文件# os.remove(pathRar)4、压缩成rar文件
由于rarfile只能解压文件不能压缩文件所以我们需要调用第三方程序来完成。
def compress(input_file, output_file, root_path,rar_pathD:/Program Files/WinRAR/WinRAR.exe):调用CMD命令压缩文件/文件夹Parameters----------input_file : 需要压缩的文件/文件夹名。从哪一级目录开始就会从哪一级开始压缩;output_file : 压缩文件的输出路径及其压缩的文件名;可以是.rar, .ziproot_path: input_file 所在目录;rar_path : WinRAR软件的安装路径,The default is C:/Program Files/WinRAR/WinRAR.exe.NOTE: 路径和文件名中带空格的时候一定要多加一重引号cmd_command r%s a %s %s % (rar_path, output_file, input_file)print(root_path)os.chdir(root_path) # 切换工作目录print(cmd_command)os.system(cmd_command)if os.system(cmd_command)0:print(Successful backup to, output_file)else:print(Backup FAILED, input_file) def rar(paths):files os.listdir(paths)for path in files:input_file path out path.split(.)[0] _bak.rarout_file out print(path)print(out)compress(input_file,out_file,paths)
参考文章https://blog.csdn.net/hanmengaidudu/article/details/120193682
三、zip文件的压缩和解压
1、安装zipfile
执行以下命令
pip install zipfile2、解压zip文件
使用zipfile的extract()或extractall()方法直接解压时文件名可能会出现乱码所以我们要特别解决这个问题
# 出现乱码时解码
def recode(raw: str) - str:try:return raw.encode(cp437).decode(gbk)except:return raw.encode(utf-8).decode(utf-8)# 解压zip文件
def decompress_zip(pathZip, obj):zipFile zipfile.ZipFile(pathZip) # 压缩包路径zipFileList zipFile.namelist() # 获取压缩包里所有文件print(-------------------正在解压-----------------------)for f in zipFileList:zipFile.extract(f, obj) # 循环解压文件到指定目录name1 os.path.join(obj, f) # 乱码文件名name2 os.path.join(obj, recode(f)) # 解码后文件名os.rename(name1, name2) # 文件重命名zipFile.close() # 关闭文件释放内存print(-------------------解压完成-----------------------)# 删除压缩文件# os.remove(pathZip)3、压缩成zip文件
参考文章https://blog.csdn.net/Likianta/article/details/126710855
参考文章https://blog.csdn.net/ooowwq/article/details/125949394
参考 文章https://blog.csdn.net/qq_36182112/article/details/127630950