Internally, there is an open file pointer called fp
that get's cleared on close; you can test for that yourself too:
if not self.zf.fp:
self.zf = zipfile.ZipFile(self.path)
See the zipfile
module source; the open
method raises the RuntimeError
exception if not self.fp
is True.
Note that relying on such internal, undocumented implementations can be hairy; if future implementations change your code will break, perhaps in subtle ways. Make sure you have good test coverage for your project.
Alternatively, you could create a ZipFile subclass and override the .close
method to track the state, which would be less at risk of breaking due to internal changes:
class MyZipFile(zipfile.ZipFile):
closed = False
def close(self):
self.closed = True
super(MyZipFile, self).close()
and
if self.zf.closed:
self.zf = MyZipFile(self.path)
with thanks to aknuds1 for the suggestion.