[Tutor] Shortening the code

Peter Otten __peter__ at web.de
Sat Nov 26 17:40:18 CET 2011


Mic wrote:

> text_file.close

It has to be 

text_file.close()

to actually do something.

> text_file=open(self.filename,"w")
> text_file.write(self.filename)
> text_file.close()
 
Pro-tip: this can be simplified to

with open(self.filename, "w") as text_file:
    text_file.write(self.filename)



More information about the Tutor mailing list