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

N

nephish

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?
 
C

Chris F.A. Johnson

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
 
J

John Machin

Chris said:
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
 
C

Chris F.A. Johnson

Chris said:
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.
 
A

Andy Leszczynski

Chris said:
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.
 
S

Steven Bethard

Andy said:
Chris said:
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
 
T

Terry Reedy

"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top