Falcolas wrote: >> f = some_file() #maybe it's the file store for a database implementation >> f.write('a bunch of stuff') >> del f >> #insert code that assumes f is closed. >> >> This is the sort of code that I warn against writing. >> >> f = some_file() >> with f: >> f.write("a bunch of stuff") >> #insert code that assumes f is closed, but correctly this time >> >> is better. > > This has raised a few questions in my mind. So, here's my newbie > question based off this. > > Is this: > > f = open(xyz) > f.write("wheee") > f.close() > # Assume file is closed properly. >
This will not immediately close f if f.write raises an exception since the program stack is kept alive as a traceback. > as "safe" as your code: > > f = some_file() > with f: > f.write("a bunch of stuff") > #insert code that assumes f is closed, but correctly this time > The with statement is designed to be safer. It contains an implicit try/finally that lets the file close itself in case of an exception. -- Lenard Lindstrom <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list