Open and closing files

Thomas Ploch Thomas.Ploch at gmx.net
Thu Nov 30 21:31:33 EST 2006


Is it defined behaviour that all files get implicitly closed when not
assigning them?

Like:

def writeFile(fName, foo):
	open(fName, 'w').write(process(foo))

compared to:


def writeFile(fName, foo):
	fileobj = open(fName, 'w')
	fileobj.write(process(foo))
	fileobj.close()

Which one is the 'official' recommended way?

Thomas



More information about the Python-list mailing list