Do you really need to use the iter function here? As far as I can tell, a file object is already an iterator. The file object documentation says that, "[a] file object is its own iterator, for example iter(f) returns f (unless f is closed)." It doesn't look like it makes a difference one way or the other, I'm just curious.
Nope, you're right -- that's just my obsessive-compulsive disorder kicking in. ;) A lot of objects aren't their own iterators, so I tend to ask for an iterator with iter() when I know I want one. But for files, this definitely isn't necessary:
py> file('temp.txt', 'w').write("""\ ... x y ... 1 2 ... 3 4 ... 5 6 ... """) py> f = file('temp.txt') py> f.next() 'x y\n' py> for line in f: ... print [float(f) for f in line.split()] ... [1.0, 2.0] [3.0, 4.0] [5.0, 6.0]
And to illustrate Alex Martelli's point that using readline, etc. before using the file as an iterator is fine:
py> f = file('temp.txt') py> f.readline() 'x y\n' py> for line in f: ... print [float(f) for f in line.split()] ... [1.0, 2.0] [3.0, 4.0] [5.0, 6.0]
But using readline, etc. after using the file as an iterator is *not* fine, generally:
py> f = file('temp.txt') py> f.next() 'x y\n' py> f.readline() ''
In this case, if my understanding's right, the entire file contents have been read into the iterator buffer, so readline thinks the entire file's been read in and gives you '' to indicate this.
Steve -- http://mail.python.org/mailman/listinfo/python-list