How to unzip a file with Python 2.4?

后端 未结 5 716
旧巷少年郎
旧巷少年郎 2020-12-08 09:42

I\'m having a hard time figuring out how to unzip a zip file with 2.4. extract() is not included in 2.4. I\'m restricted to using 2.4.4 on my server.

Ca

5条回答
  •  半阙折子戏
    2020-12-08 10:36

    There's some problem with Vinko's answer (at least when I run it). I got:

    IOError: [Errno 13] Permission denied: '01org-webapps-countingbeads-422c4e1/'
    

    Here's how to solve it:

    # unzip a file
    def unzip(path):
        zfile = zipfile.ZipFile(path)
        for name in zfile.namelist():
            (dirname, filename) = os.path.split(name)
            if filename == '':
                # directory
                if not os.path.exists(dirname):
                    os.mkdir(dirname)
            else:
                # file
                fd = open(name, 'w')
                fd.write(zfile.read(name))
                fd.close()
        zfile.close()
    

提交回复
热议问题