On Thu, Apr 30, 2015 at 4:27 PM, Cecil Westerhof <ce...@decebal.nl> wrote: >> with open("input.cpp") as f: >> lines = f.readlines() >> print(lines[7]) > > Is the following not better: > print(open('input.cpp', 'r').readlines()[7]) > > Time is the same (about 25 seconds for 100.000 calls), but I find this > more clear.
The significant difference is that the 'with' block guarantees to close the file promptly. With CPython it probably won't make a lot of difference, and in a tiny script it won't do much either, but if you do this on Jython or IronPython or MicroPython or some other implementation, it may well make a gigantic difference - your loop might actually fail because the file's still open. ChrisA -- https://mail.python.org/mailman/listinfo/python-list