Extracting zip files containing directories with ZipFile

Márcio Faustino m.faustino at gmail.com
Sun Apr 12 17:44:45 CEST 2009


Does the ZipFile class correctly handles directories contained within
zip files?

For example, calling "extractall" on an archive with only "folder/
file.txt" in it results in an IOError saying "No such file or
directory" for "./folder/file.txt", because it created a file named
"folder" instead of a directory. (I've tested this with version 2.6.1
on Windows XP.) However, changing that method to the following, seems
to solve this particular problem:

def extractall(self, path = os.path.curdir, members = None, password =
    if members is None:
        members = self.namelist()

    # Place directories first to create them before extracting any

    for name in members:
        if name.endswith('/'):
            os.makedirs(os.path.join(path, name))
            self.extract(name, path, password)


More information about the Python-list mailing list