[CentralOH] Files and context manager
Mark Erbaugh
mark at microenh.com
Wed Mar 10 21:39:19 CET 2010
>
> A common idiom with older versions of Python is:
>
> x = open(filename,'rb').read()
>
> With context managers in 2.6+ you could say:
>
> with open(filename,'rb') as f:
> x = f.read()
>
> Is the later safer in that it explicitly will call close() whereas the former depends on the file destructor to close the file?
>
I forgot the second half of my question.
Is a function like:
def getData(filename):
with open(filename,'rb') as f:
return f.read()
a good idea?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20100310/dd157bf4/attachment.html>
More information about the CentralOH
mailing list