Populating a list

Luke LLoeffler at home.com
Sun Dec 9 19:24:27 EST 2001


> states = []
> f=open('/home/mlorfeld/states.txt', 'r+').readlines()
> for i in f:
>   states.append(i.rstrip())
> f.close()
> print states

f.close() will fail because f is a list, not a file object.

 > f=open('/home/mlorfeld/states.txt', 'r+').readlines()

This line while being fine, probably isn't the best idea in the interest 
of clairity as f traditionally denotes a file pointer, not a list.  I 
would give it a more meaningful name.




More information about the Python-list mailing list