[CentralOH] Files and context manager

Mark Erbaugh mark at microenh.com
Wed Mar 10 21:34:07 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?


More information about the CentralOH mailing list