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

Chris cwitts at gmail.com
Thu Aug 28 06:01:56 EDT 2008


On Aug 28, 11:53 am, Ken Starks <stra... at lampsacos.demon.co.uk> wrote:
> youngjin.mich... at gmail.com wrote:
> > Hello,
>
> > I am new to Python and have one simple question to which I cannot find
> > a satisfactory solution.
> > 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.
>
> > file = open(fileName, 'r')
> > lineNumber = 0
> > for line in file:
> >     if lineNumber == 0:
> >         lineNumber = lineNumber + 1
> >     else:
> >         lineNumber = lineNumber + 1
> >         print line
>
> > Can anyone show me the better of doing this kind of task?
>
> > Thanks in advance.
>
> LineList=open(filename,'r').readlines()[1,]
> for line in Linelist:
>     blah blah

That's bad practice as you load the entire file in memory first as
well as it will result in a type error (should be '.readlines()[1:]')



More information about the Python-list mailing list