469,929 Members | 1,889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

How do i read just the last line of a text file?

Hey there.
i want to set a variable to represent the last line of a text file
how do i do that?
or even better, how do i create a list of the lines of a text file?

Jul 19 '05 #1
8 2160
nephish wrote:
Hey there.
i want to set a variable to represent the last line of a text file
how do i do that?
or even better, how do i create a list of the lines of a text file?


Hey there to you too.

According to the manual
http://www.python.org/doc/2.4.1/lib/...e-objects.html

readlines([sizehint])
Read until EOF using readline() and return a list containing
the lines thus read.

Which part of this don't you understand?

Jul 19 '05 #2
On Sun, 29 May 2005 at 04:42 GMT, nephish wrote:
Hey there.
i want to set a variable to represent the last line of a text file
how do i do that?
or even better, how do i create a list of the lines of a text file?


from sys import argv ## Import argv from sys module

file = open(argv[1]) ## Open the file given on the command line
all_lines = file.readlines() ## Read all the lines
last_line = all_lines[-1] ## Assign the last line
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
================================================== ================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Jul 19 '05 #3
Chris F.A. Johnson wrote:

file = open(argv[1]) ## Open the file given on the command line
all_lines = file.readlines() ## Read all the lines


I see your shadowing and raise you one obfuscation:

open = file(argv[1]) ## File the open given on the command line
all_lines = open.readlines() ## Read all the lines
Jul 19 '05 #4
On Sun, 29 May 2005 at 05:57 GMT, John Machin wrote:
Chris F.A. Johnson wrote:
file = open(argv[1]) ## Open the file given on the command line
all_lines = file.readlines() ## Read all the lines
I see your shadowing and raise you one obfuscation:


;)
open = file(argv[1]) ## File the open given on the command line
all_lines = open.readlines() ## Read all the lines


Such verbosity! (I excuse mine on the grounds that it was my first
attempt at a Python program.)

all_lines = file(argv[1]).readlines()

And to answer the question in the subject line:

last_line = file(argv[1]).readlines()[-1]

Both of which assume "from sys import argv".
Now I have to get serious and forget those bad habits.

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
================================================== ================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Jul 19 '05 #5
Chris F.A. Johnson wrote:
On Sun, 29 May 2005 at 05:57 GMT, John Machin wrote:
Chris F.A. Johnson wrote:

file = open(argv[1]) ## Open the file given on the command line
all_lines = file.readlines() ## Read all the lines


I see your shadowing and raise you one obfuscation:

;)

open = file(argv[1]) ## File the open given on the command line
all_lines = open.readlines() ## Read all the lines

Such verbosity! (I excuse mine on the grounds that it was my first
attempt at a Python program.)

all_lines = file(argv[1]).readlines()

And to answer the question in the subject line:

last_line = file(argv[1]).readlines()[-1]

Both of which assume "from sys import argv".
Now I have to get serious and forget those bad habits.


What if a file is long enough?

A.
Jul 19 '05 #6
Andy Leszczynski wrote:
Chris F.A. Johnson wrote:
And to answer the question in the subject line:

last_line = file(argv[1]).readlines()[-1]

Both of which assume "from sys import argv".


What if a file is long enough?


Huh? You mean what if it's too big to fit in memory? Then try this:

for last_line in file(argv[1]):
pass

At the end of the for loop, last_line should be bound to the last line
in the file. (If there's a chance your file might not have any lines,
you'll want to do some error checking...)

STeVe
Jul 19 '05 #7
cool. thanks for the help guys !

Jul 19 '05 #8

"Andy Leszczynski"
What if a file is long enough?


I believe you meant "What if a file is too long to read all into memory at
once?"

If the file is randomly accessible (with file.seek() backwards from the
end) then you can read a chunk at the end that you expect to be large
enough to contain the last line and search backwards for \n (ignoring a
terminating \n) to find the end of the next-to-last line. Even if the file
will fit in memory, this may be faster.

Terry J. Reedy

Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by sea | last post: by
3 posts views Thread by John Flynn | last post: by
35 posts views Thread by RyanS09 | last post: by
9 posts views Thread by Adi | last post: by
9 posts views Thread by =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post: by
4 posts views Thread by Keith G Hicks | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.