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

Chad Crabtree flaxeater at gmail.com
Thu Oct 19 21:11:38 CEST 2006


On 10/19/06, Chris Hengge <pyro9219 at gmail.com> wrote:
>
> 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.


This works as well

file('filename.txt').readlines()[-1]

Some will say that this is no good because the file is still open.  However
I've been told that when the object is cleaned the file is closed so it
should be fine.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061019/9890c1c2/attachment.htm 


More information about the Tutor mailing list