Unzipping directory structure with python

后端 未结 9 1656
闹比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条回答
  •  Happy的楠姐
    2020-12-13 01:24

    If like me, you have to extract a complete zip archive with an older Python release (in my case, 2.4) here's what I came up with (based on Jeff's answer):

    import zipfile
    import os
    
    def unzip(source_file_path, destination_dir):
        destination_dir += '/'
        z = zipfile.ZipFile(source_file_path, 'r')
        for file in z.namelist():
            outfile_path = destination_dir + file
            if file.endswith('/'):
                os.makedirs(outfile_path)
            else:
                outfile = open(outfile_path, 'wb')
                outfile.write(z.read(file))
                outfile.close()
        z.close()
    

提交回复
热议问题