在python中重命名和提取zipfile [英] Rename and extract zipfile in python

查看:40
本文介绍了在python中重命名和提取zipfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Wanted to extract .zip file one by one. Before extracting I need to rename

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/没有压缩结构

The above code extracts all file in /tmp/. But I wanted to rename each file and save in destination directory ie., /tmp/ without zipped structure

推荐答案

包含read函数后,可以操作文件名

After including read function, I can manipulate the file name

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

这篇关于在python中重命名和提取zipfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆