Python achieves compression, decompression of files, and password decompression of files.

The file is too large and the transfer is too slow. I will teach you how to compress files today.

 

 

1. The cause of the incident

       First, let's talk about the cause of the incident. I am going to pass a bunch of my files to a colleague, but I don’t have a tool to compress files, what should I do? Eh! Why don't you write one yourself, and you don't have to download one separately for it. Isn't it beautiful? If you have an idea, you must act quickly.

 

 

2. Ideas and materials

01. First, let's talk about our ideas.

I need to compress many files and pack them into a compressed package. We also need to freely decompress the compressed package we have packed.

File-->Compress-->Compress Package-->Unzip

 

02. Let's talk about preparing materials.

1. A computer with python operating environment.

2. Install the zipfile module. pip install zipfile

3. Prepare files to be compressed.

 

 

Three, coding realization

import zipfile
​
"""压缩文件操作"""
files=['zip/report.jmx',"2号文件"]#压缩文件路径
zpf=zipfile.ZipFile("压缩.zip",'w', zipfile.ZIP_DEFLATED)
for file in files:#循环写入压缩文件
    zpf.write(file)
zpf.close()#关闭文件
print('压缩完成')
"""无密码时解压操作"""
zpf= zipfile.ZipFile("zip.zip")
list = zpf.namelist()  # 得到压缩包里所有文件
for f in list:
    print(f)
    zpf.extract(f, "zip")  # 循环解压文件到指定目录
zpf.close()  # 关闭文件,释放内存
"""有密码时解压操作"""
zpf= zipfile.ZipFile("zip.zip")
list = zpf.namelist()  # 得到压缩包里所有文件
for f in list:
    zpf.extract(f, "zip", b"密码")  # 循环解压文件到指定目录,密码
print("解压成功")

 

That's it, our compression and decompression operations are complete, and we can show our colleagues from now on. We are still using compression software, we all wrote it ourselves!

Scan the QR code at the bottom and follow the official account to get more source code.

 

Hidden confession skills, python teaches you how to use picture exif information to hide confession

Say goodbye to drop-down, easily operate excel

Quickly create your own WeChat chat tool with zero foundation

Two-dimensional code confession of the little secrets in the two-dimensional code

 

 

 

Guess you like

Origin blog.csdn.net/qq_39046854/article/details/107371019