Populating a list

Jeremy Whetzel lists at toadmail.com
Mon Dec 10 18:19:53 EST 2001


kevin at cazabon.com (Kevin Cazabon) writes:

> The reason you'd get an error is that you have no handle on the open
> file.  (f is a list, not a file pointer).  The file is still open
> however, you just have no way to close it.  I'd hope that Python would
> close the file once it goes out of scope, but I couldn't guarantee it.
> 
> To be safe, I'd suggest doing the open and readlines separately, so
> you can keep a reference to the file, then use it to close the file.
> 
> f = open(filename, "r")
> crap = [item.rstrip() for item in f.readlines()]
> f.close()

Ahhhh... I got it.  Thank you.  I really thought it closed the file
because of the readlines() on the end of the 'open' function.  *scurries
off to fix current project*

Jeremy



More information about the Python-list mailing list