• Python中zipfile压缩文件模块的使用


    目录

    zipfile

    压缩一个文件

    解压文件

    高级应用

    利用 zipfile 模块破解压缩文件口令:Python脚本破解压缩文件口令


    zipfile

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

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

    mode的几种:

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

    压缩一个文件

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

    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  文件了

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

    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字符串的值。

    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()

    高级应用

    zipfile.is_zipfile(filename) 
    判断一个文件是不是压缩文件 
    ZipFile.namelist() 
    返回文件列表 

    if zipfile.is_zipfile('test.zip'): #is_zipfile() 判断是否似zip文件
       f = zipfile.ZipFile('test.zip')
       files = f.namelist() #namelist() 返回zip压缩包中的所有文件
       print(files)
       f.close()

    利用 zipfile 模块破解压缩文件口令:Python脚本破解压缩文件口令

  • 相关阅读:
    cubestore driver 添加auth认证
    cubestore 添加auth 认证
    基于s3 扩展cubestore
    cube.js 预聚合检查
    cube.js 集成cubestore 时间格式问题的一些说明
    cube.js 集成cubestore 时间格式问题
    Building a GraphQL to SQL Compiler on Postgres, MS SQL and MySQL
    支持minio cubestore docker 镜像
    数据治理怎么做
    使用project制定项目计划可以分为六个步骤
  • 原文地址:https://www.cnblogs.com/csnd/p/11807853.html
Copyright © 2020-2023  润新知