Search code examples
python-3.xzip

Having trouble using zipfile.ZipFile.extractall (Already read the docs)


I have a folder with many zipfiles, most of these zipfiles contain shapefiles and some of them have subfolders which contain zipfiles that contain shapefiles. I am trying to extract everything into one main folder wihtout keeping any folder structure. This is where I am now;

    import os, zipfile

def getListOfFiles(dirName):
    # create a list of file and sub directories
    # names in the given directory
    listOfFile = os.listdir(dirName)
    allFiles = list()
    # Iterate over all the entries
    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)
        # If entry is a directory then get the list of files in this directory
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)

    return allFiles


def main():
    dirName = r'C:\Users\myusername\My_Dataset'

    # Get the list of all files in directory tree at given path
    listOfFiles = getListOfFiles(dirName)

    # Print the files
    for elem in listOfFiles:
        print(elem)
        zipfile.ZipFile.extractall(elem)

    print("****************")


if __name__ == '__main__':
    main()

This script prints all the shapefiles (including the ones under subfolders). Now I need to extract all these listed shapefiles into one main folder. I try zipfile.ZipFile.extractall(elem) but it doesn't work.

line 1611, in extractall
    members = self.namelist()
AttributeError: 'str' object has no attribute 'namelist'

Is the error I'm getting. zipfile.ZipFile.extractall(elem) is the line that doesn't work. I imagine it expects one zipfile but I'm trying to feed it a folder (or a list in this case?)

How would I change this script so that it extracts my listed shapefiles into a folder (preferably a new folder)


Solution

  • You need to make an instance of ZipFile first and use extractall on this instance:

    for elem in listOfFiles:
         my_zipfile = zipfile.ZipFile(elem)
         my_zipfile.extractall()