On 20 January 2012 15:44, Andrea Crotti <andrea.crott...@gmail.com> wrote: > I normally didn't bother too much when reading from files, and for example > I always did a > > content = open(filename).readlines() > > But now I have the doubt that it's not a good idea, does the file handler > stays > open until the interpreter quits?
IIRC it stays open until garbage collected (it's closed in the file object's __del__ method). When the file object is collected is an implementation detail, although in CPython it will happen straight away because it uses reference counting. > So maybe doing a > > with open(filename) as f: > contents = f.readlines() That's what I do, unless I'm in an interactive session. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list