for line in open(filename)

Posted by foosion on Stack Overflow See other posts from Stack Overflow or by foosion
Published on 2009-09-25T17:47:07Z Indexed on 2010/05/28 2:01 UTC
Read the original article Hit count: 221

I frequently see python code similar to

for line in open(filename):
    do_something(line)

When does filename get closed with this code?

Would it be better to write

with open(filename) as f:
    for line in f.readlines():
        do_something(line)

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner