在python中重命名和解压缩zipfile

时间:2015-07-20 10:38:18

标签: python zipfile

想要逐个提取.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 /没有压缩结构

1 个答案:

答案 0 :(得分: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()