Based on the with statement
I've added this as another answer because it is generally not an answer to initial question. However, it can help to fix your problem.
class MyZipFile(zipfile.ZipFile): # Create class based on zipfile.ZipFile def __init__(file, mode='r'): # Initial part of our module zipfile.ZipFile.__init__(file, mode) # Create ZipFile object def __enter__(self): # On entering... return(self) # Return object created in __init__ part def __exit__(self, exc_type, exc_val, exc_tb): # On exiting... self.close() # Use close method of zipfile.ZipFile
with MyZipFile('new.zip', 'w') as tempzip: # Use content manager of MyZipFile tempzip.write('sbdtools.py') # Write file to our archive
If you type
you can see all methods of original zipfile.ZipFile and your own methods: init, enter and exit. You can add another own functions if you want. Good luck!