How do i read just the last line of a text file?
Steven Bethard
steven.bethard at gmail.com
Sun May 29 11:20:37 EDT 2005
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
More information about the Python-list
mailing list