[CentralOH] Files and context manager

Mark Erbaugh mark at microenh.com
Wed Mar 10 21:34:07 CET 2010


Hi,

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?

Thanks,
Mark


More information about the CentralOH mailing list