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

josh logan dear.jay.logan at gmail.com
Sat Aug 30 07:36:07 EDT 2008


On Aug 28, 3:47 am, Santiago Romero <srom... at gmail.com> wrote:
> > I want to read text line-by-line from a text file, but want to ignore
> > only the first line. I know how to do it in Java (Java has been my
> > primary language for the last couple of years) and following is what I
> > have in Python, but I don't like it and want to learn the better way
> > of doing it.
>
>  Why don't you read and discard the first line before processing the
> rest of the file?
>
>  file = open(filename, 'r')
>  file.readline()
>  for line in file: print line,
>
>  (It works).

# You could also do the following:

from itertools import islice

f = open(filename)

for each_line in islice(f, 1, None):
	print line



More information about the Python-list mailing list