2

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

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
jOSe
  • 687
  • 11
  • 22

1 Answers1

2

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()
jOSe
  • 687
  • 11
  • 22