python之zipfile应用

zipfile

Python 中 zipfile 模块提供了对 zip 压缩文件的一系列操作。

1

f=zipfile.ZipFile("test.zip",mode="")  //解压是 r , 压缩是 w 追加压缩是 a

mode的几种:

  • 解压:r
  • 压缩:w
  • 追加压缩:

压缩一个文件

创建一个压缩文件 test.zip(如果test.zip文件不存在) ,然后将 test.txt 文件加入到压缩文件 test.zip 中,如果原来的压缩文件中有内容,会清除原有的内容

1

2

3

4

5

6

7

8

9

import zipfile

try:

  with zipfile.ZipFile("c://users//17250//desktop//test.zip",mode="w") as f:

    f.write("c://users//17250//desktop//test.txt")          #写入压缩文件,会把压缩文件中的原有覆盖

except Exception as e:

    print("异常对象的类型是:%s"%type(e))

    print("异常对象的内容是:%s"%e)

finally:

    f.close()

如果要压缩的文件的路径是 c://users//17250//desktop//test.txt 这样的话,

那么最后压缩文件里面压缩的就是  users//17250//desktop//test.txt  文件了

 

向已存在的压缩文件中追加内容

1

2

3

4

5

6

7

8

9

import zipfile

try:

  with zipfile.ZipFile("c://users//17250//desktop//test.zip",mode="a") as f:

    f.write("e://test.txt")          #追加写入压缩文件

except Exception as e:

    print("异常对象的类型是:%s"%type(e))

    print("异常对象的内容是:%s"%e)

finally:

    f.close()

虽然原文件里面压缩的文件的路径是 users//17250//desktop//test.txt  ,但是追加进去的是 e://test2.txt 文件,那么test2.txt 文件压缩是在 users 那一级的目录。

 

 

解压文件

test.zip文件解压

python3中,解压文件的密码参数 pwd 接收的是二进制的值,所以要在前面加一个 b 。python2中接受的是str字符串的值。

1

2

3

4

5

6

7

8

9

import zipfile

try:

  with zipfile.ZipFile("c://users//17250//desktop//test.zip",mode="a") as f:

     f.extractall("c://users//17250//desktop//",pwd=b"root") ##将文件解压到指定目录,解压密码为root

except Exception as e:

     print("异常对象的类型是:%s"%type(e))

     print("异常对象的内容是:%s"%e)

finally:

     f.close()

posted @ 2022-01-08 21:44  菩提浪子  阅读(651)  评论(0)    收藏  举报