Python实现批量压缩解压文件(zip、rar)
好长一段时间没有更新了,最近手里的项目没停过太忙了。今天正好有空就分享下之前编写的一段代码。
起因是因为我想将一些文件上传至网盘,但奈何太穷开不起会员,无法上传超过4G的文件。于是就想能不能使用Python实现批量压缩文件夹,搞都搞了顺便将解压文件夹也写出来了。今天就和大家分享一下。
一、导入相关库
os库是为了监测生成的文件夹是否已存在。主要的库是zipfile,它提供了有关windows下的文件/文件夹的压缩、解压的函数。
import os import zipfile
二、压缩文件
今天偷个懒,就不一段一段的去解释了,直接整个函数展示。相关的注释已经写清楚了,主要函数就是zipfile.ZipFile和zip_file.write。zip_file.write(file_path,file_each)中,file_path是文件夹中文件的完整路径,file_each是文件名。
def Compress_path_zip(path_all):
path_all_list = os.listdir(path_all)
# 列出总文件夹内所有需要压缩的文件夹
for path_each in path_all_list:
path_compress = os.path.join(path_all, path_each)
# 待压缩的绝对路径
if os.path.isdir(path_compress):
print("正在压缩:%s" % path_each)
# 遍历所有需要压缩的文件夹
if os.path.exists(path_all + "%s.zip" % path_each):
print(path_all + "%s1.zip" % path_each+"已存在,新文件名为'%s'" % (path_all + "%s1.zip" % path_each))
zip_file = zipfile.ZipFile(path_all + "%s1.zip" % path_each, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)
# 创建的压缩对象用于执行后续操作
# file_list = zip_file.namelist()
# 列出压缩文件夹中所有文件
else:
zip_file = zipfile.ZipFile(path_all + "%s.zip" % path_each, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)
file_list = os.listdir(path_compress)
# 待压缩文件夹内所有文件
# zip_file.setpassword(b'123')
for file_each in file_list:
# 遍历所有文件夹内的文件
file_path = os.path.join(path_compress, file_each)
zip_file.write(file_path, file_each)
zip_file.close()
else:
print(path_each, "不是文件夹,不进行压缩")
continue三、解压文件
这段代码就不细说了,反正修改了路径就能直接使用了。
def Decompress_path_zip(path_all):
path_all_list = os.listdir(path_all)
for path_each in path_all_list:
# 遍历所有需要压缩的文件夹
if path_each.endswith('.zip'):
print("正在解压:%s" % path_each)
path_decompress = os.path.join(path_all, path_each)
zip_file = zipfile.ZipFile(path_decompress, 'r') # 压缩文件位置
for file in zip_file.namelist():
if os.path.exists(path_decompress[:-4]):
print("'%s'" % path_decompress[:-4], "已存在,新文件名为'%s'" % (path_decompress[:-4] + "1"))
zip_file.extract(file, path_decompress[:-4] + "1") # 解压位置,pwd="1234".encode('utf-8')
else:
zip_file.extract(file, path_decompress[:-4]) # 解压位置
zip_file.close()
else:
print(path_each, "非zip文件,不进行解压!")
continue四、完整代码
# -*- coding: utf-8 -*-
"""
@Time : 2023/8/28 14:47
@Auth : RS迷途小书童
@File :Compress and Decompress Folders.py
@IDE :PyCharm
@Purpose:批量压缩/解压文件夹
"""
import os
import zipfile
def Compress_path_zip(path_all):
path_all_list = os.listdir(path_all)
# 列出总文件夹内所有需要压缩的文件夹
for path_each in path_all_list:
path_compress = os.path.join(path_all, path_each)
# 待压缩的绝对路径
if os.path.isdir(path_compress):
print("正在压缩:%s" % path_each)
# 遍历所有需要压缩的文件夹
if os.path.exists(path_all + "%s.zip" % path_each):
print(path_all + "%s1.zip" % path_each+"已存在,新文件名为'%s'" % (path_all + "%s1.zip" % path_each))
zip_file = zipfile.ZipFile(path_all + "%s1.zip" % path_each, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)
# 创建的压缩对象用于执行后续操作
# file_list = zip_file.namelist()
# 列出压缩文件夹中所有文件
else:
zip_file = zipfile.ZipFile(path_all + "%s.zip" % path_each, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)
file_list = os.listdir(path_compress)
# 待压缩文件夹内所有文件
# zip_file.setpassword(b'123')
for file_each in file_list:
# 遍历所有文件夹内的文件
file_path = os.path.join(path_compress, file_each)
zip_file.write(file_path, file_each)
zip_file.close()
else:
print(path_each, "不是文件夹,不进行压缩")
continue
def Decompress_path_zip(path_all):
path_all_list = os.listdir(path_all)
for path_each in path_all_list:
# 遍历所有需要压缩的文件夹
if path_each.endswith('.zip'):
print("正在解压:%s" % path_each)
path_decompress = os.path.join(path_all, path_each)
zip_file = zipfile.ZipFile(path_decompress, 'r') # 压缩文件位置
for file in zip_file.namelist():
if os.path.exists(path_decompress[:-4]):
print("'%s'" % path_decompress[:-4], "已存在,新文件名为'%s'" % (path_decompress[:-4] + "1"))
zip_file.extract(file, path_decompress[:-4] + "1") # 解压位置,pwd="1234".encode('utf-8')
else:
zip_file.extract(file, path_decompress[:-4]) # 解压位置
zip_file.close()
else:
print(path_each, "非zip文件,不进行解压!")
continue
if __name__ == "__main__":
path = "G:/try/"
# Compress_path_zip(path)
Decompress_path_zip(path)本来是想将压缩加密文件和解压加密文件写出来的,结果zipfile库好像不支持加密文件,所以就没写。虽然解压加密文件可以,但是很呆,因为只有使用传统加密的zip文件,才可以使用密码解压,不是传统加密的就算密码正确他也不解压,所以就都不写啦。大家如果想用的话,直接修改路径就行。
到此这篇关于Python实现批量压缩解压文件(zip、rar)的文章就介绍到这了,更多相关Python 批量压缩解压文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Python的collections模块中的OrderedDict有序字典
字典是无序的,但是collections的OrderedDict类为我们提供了一个有序的字典结构,名副其实的Ordered+Dict,下面通过两个例子来简单了解下Python的collections模块中的OrderedDict有序字典:2016-07-07
tf.truncated_normal与tf.random_normal的详细用法
本篇文章主要介绍了tf.truncated_normal与tf.random_normal的详细用法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-03-03
pandas dataframe中双中括号和单中括号的区别及说明
这篇文章主要介绍了pandas dataframe中双中括号和单中括号的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-08-08
Python实现网络通信的HTTP请求Socket编程Web爬虫方法探索
随着互联网的不断发展,Python作为一门多用途的编程语言,提供了强大的工具和库来进行网络连接和通信,本文将深入探讨Python中连接网络的方法,包括HTTP请求、Socket编程、Web爬虫和REST API的使用2024-01-01


最新评论