zipfile module and exceptions

Skip Montanaro skip at pobox.com
Mon Jan 27 12:07:48 EST 2003


    >> The problem is that in the except clause the z.close() fails because
    >> the z is not known. Am I not supposed to close the file in the case
    >> of IOError? If not the solution is simple... Any ideas?

Try something like this instead:

    try:
        z = zipfile.ZipFile(file, "r")
    except (possible creation errors here):
        print >> sys.stderr, "Can't open zipfile %s for reading" % file
    else:
        if z.testzip() is None:
            self.listBox2.Append(file)
        else:
            self.listBox3.Append(file)
        z.close()

Skip





More information about the Python-list mailing list