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

2005-05-29 Thread 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



-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-05-29 Thread nephish
cool. thanks for the help guys !

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-05-29 Thread Steven Bethard
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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-05-29 Thread Andy Leszczynski
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-05-29 Thread Chris F.A. Johnson
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 
==
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-05-28 Thread John Machin
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


-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-05-28 Thread Chris F.A. Johnson
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 
==
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-05-28 Thread John Machin
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/bltin-file-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?

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-05-28 Thread 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?

-- 
http://mail.python.org/mailman/listinfo/python-list