【问题标题】:Rename and extract zipfile in python在python中重命名和提取zipfile
【发布时间】:2022-05-02 06:39:23
【问题描述】:

想一一解压.zip 文件。在提取之前我需要重命名

myzip = zipfile.ZipFile(source,'r')
    for zib_e in myzip.namelist():
        filename = os.path.basename(zib_e)
        if not filename:
            continue
        print zib_e
        myzip.extract(zib_e,"/tmp/")
    myzip.close()

以上代码提取 /tmp/ 中的所有文件。但我想重命名每个文件并保存在目标目录中,即 /tmp/ 没有压缩结构

【问题讨论】:

    标签: python python-zipfile


    【解决方案1】:

    包含读取功能后,我可以操作文件名

    def guid1():
        uniqueid = uuid.uuid4()
        guid = str(uniqueid)
        return guid
    
    def zipextract(source,destination):
        myzip = zipfile.ZipFile(source,'r')
        for zib_e in myzip.namelist():
            filename = os.path.basename(zib_e)
            if not filename:
                continue
            print destination
            data = myzip.read(zib_e)
            output = open(destination+guid1()+".txt",'wb') #exporting to given location one by one
            output.write(data)
            output.close()
            #data.close()
        myzip.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 2014-01-31
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      相关资源
      最近更新 更多