python - with open (name,"rb") as f need not close am i right? -


i notice can open file this:

f=open("a.dat","rb") 

and method:

with open("a.dat","rb") f: 

in opinion, if use first one, must call f.close() function, while second method need not. right? or there better method? 1 best open file in python? thanks;-)

yes, not need close file handled with block. if exception occurs before end of block, close file before exception caught outer exception handler.

since python 2.5 (when with statement introduced) using with statement file operations preferable way.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -