Question> I have checked the source code of zipfile located here:
/usr/lib/python2.6/zipfile.py
我不知道对<代码>__enter__和__exit
功能的界定如何?
Thank you
Question> I have checked the source code of zipfile located here:
/usr/lib/python2.6/zipfile.py
我不知道对<代码>__enter__和__exit
功能的界定如何?
Thank you
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
如果是,
help(MyZipFile)
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!
zipfile.ZipFile
is not a context Manager in 2.6, this has been supplemented in 2.7.
a. 使用物体类别创建班级的实例:
class ZipExtractor(object): # Create class that can only extract zip files
def __init__(self, path): # Initial part
import zipfile # Import old zipfile
self.Path = path # To make path available to all class
try: open(self.Path, rb ) # To check whether file exists
except IOError: print( File doesn t exist ) # Catch error and print it
else: # If file can be opened
with open(self.Path, rb ) as temp:
self.Header = temp.read(4) # Read first 4 bytes
if self.Header != x50x4Bx03x04 :
print( Your file is not a zip archive! )
else: self.ZipObject = zipfile.ZipFile(self.Path, r )
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 our class
def SuperExtract(member=None, path=None):
Used to extract files from zip archive. If arg member
was not set, extract all files. If path was set, extract file(s)
to selected folder.
print( Extracting ZIP archive %s % self.Path) # Print path of zip
print( Archive has header %s % self.Header) # Print header of zip
if filename=None:
self.ZipObject.extractall(path) # Extract all if member was not set
else:
self.ZipObject.extract(mamber, path) # Else extract selected file
def close(self): # To close our file
self.ZipObject.close()
使用:
with ZipExtractor( /path/to/zip ) as zfile:
zfile.SuperExtract( file ) # Extract file to current dir
zfile.SuperExtract(None, path= /your/folder ) # Extract all to selected dir
# another way
zfile = ZipExtractor( /path/to/zip )
zfile.SuperExtract( file )
zfile.close() # Don t forget that line to clear memory
如果你得到帮助,你将看到五种方法:
__init__, __enter__, __exit__, close, SuperExtract
我希望我能帮助你。 我没有试过,因此,你可能必须加以改进。
散射加盈余是正确的。 但是,如果你想要的话,你可以写上你自己的班子,添加“被允许的”特征。 你们都需要做的是,在你们的班子中增加两个职能(根据手稿):
def __enter__(self):
return(self)
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
这一点应当足够了,AFAIR。
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...