[Tutor] Trying to extract the last line of a text file

Chris Hengge pyro9219 at gmail.com
Thu Oct 19 20:58:18 CEST 2006


I'd personally do something like this.

file = open(myfile, 'r')
fileContents = file.readlines() # read the entire document into memory for
speed.
file.close()

print fileContents[-1] # This is the last line.

On 10/19/06, Asrarahmed Kadri <ajkadri at googlemail.com> wrote:
>
> My algorithm is like this:
> first count the number of lines in the file by using a loop.
> Use a second loop and when teh counter reaches the num_of_lines values:
> take the line.
>
> Is there any other way to do it??
>
>
>
> On 10/19/06, Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
> >
> >
> >
> > On Thu, 19 Oct 2006, Asrarahmed Kadri wrote:
> >
> > > I want to extract the last line of the text file. Any idea ???
> >
> > Hi Asrarahmed,
> >
> > Ok, so what part do you get stuck on when you try to do this?  That is,
> > what sort of things do you already know how to do with files?
> >
>
>
>
> --
> To HIM you shall return.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061019/467a3193/attachment.html 


More information about the Tutor mailing list