[Q] How to ignore the first line of the text read from a file

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Aug 28 20:00:57 EDT 2008


On Thu, 28 Aug 2008 15:11:39 -0700, Dennis Lee Bieber wrote:

> On 28 Aug 2008 19:32:45 GMT, Marc 'BlackJack' Rintsch <bj_666 at gmx.net>
> declaimed the following in comp.lang.python:
> 
>> On Thu, 28 Aug 2008 10:16:45 -0700, norseman wrote:
>> > import os
>> > 
>> > file = open(filename, 'r')
>> > for line in file:
>> >    dummy=line
>> >    for line in file:
>> >      print line
>> > 
>> > 
>> > is cleaner and faster.
>> 
>> That's not cleaner, that's a 'WTF?'!  A ``for`` line over `file` that
>> does *not* iterate over the file but is just there to skip the first
>> line and a completely useless `dummy` name.  That's seriously ugly and
>> confusing.
>>
> 	Nice to see someone else was as, uhm, offended by that code sample
> as I was -- I just lacked the vocabulary to put it across cleanly, so
> didn't respond.
> 
> 	Yes, the "dummy" statement could be completely dropped to the same
> effect -- still leaving the useless outer loop...

Nevertheless, I've just done some timeit tests on the two code snippets, 
and to my *great* surprise the second ugly snippet is consistently a 
smidgen faster even with the pointless import and dummy statement left in.

That is so counter-intuitive that I wonder whether I've done something 
wrong, or if it's some sort of freakish side-effect of disk caching or 
something. But further investigation will have to wait for later.

If anyone wants to run their own timing tests, don't forget to close the 
file explicitly, otherwise timeit() will (I think...) simply iterate over 
the EOF for all but the first iteration.



-- 
Steven



More information about the Python-list mailing list