Skip to content Skip to sidebar Skip to footer

Extract Zip File Without Folder Python

I am currently using extratall function in python to unzip, after unziping it also creates a folder like: myfile.zip -> myfile/myfile.zip , how do i get rid of myfile flder and

Solution 1:

I use the standard module zipfile. There is the method extract which provides what I think you want. This method has the optional argument path to either extract the content to the current working directory or the the given path

import os, zipfile

os.chdir('path/of/my.zip')

with zipfile.ZipFile('my.zip') as Z :
    for elem in Z.namelist() :
        Z.extract(elem, 'path/where/extract/to')

If you omit the 'path/where/extract/to' the files from the ZIP-File will be extracted to the directory of the ZIP-File.

Solution 2:

import shutil

# loop over everything in the zipfor name in myzip.namelist():
    # open the entry so we can copy it
    member = myzip.open(name)
    withopen(os.path.basename(name), 'wb') as outfile:
        # copy it directly to the output directory,# without creating the intermediate directory
        shutil.copyfileobj(member, outfile)

Post a Comment for "Extract Zip File Without Folder Python"