Unzipping directory structure with python

后端 未结 9 1652
闹比i
闹比i 2020-12-13 00:56

I have a zip file which contains the following directory structure:

dir1\\dir2\\dir3a
dir1\\dir2\\dir3b

I\'m trying to unzip it and maintai

9条回答
  •  轮回少年
    2020-12-13 01:13

    There's a very easy way if you're using Python 2.6: the extractall method.

    However, since the zipfile module is implemented completely in Python without any C extensions, you can probably copy it out of a 2.6 installation and use it with an older version of Python; you may find this easier than having to reimplement the functionality yourself. However, the function itself is quite short:

    def extractall(self, path=None, members=None, pwd=None):
        """Extract all members from the archive to the current working
           directory. `path' specifies a different directory to extract to.
           `members' is optional and must be a subset of the list returned
           by namelist().
        """
        if members is None:
            members = self.namelist()
    
        for zipinfo in members:
            self.extract(zipinfo, path, pwd)
    

提交回复
热议问题