Re: How do i read just the last line of a text file?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

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

From: Andy Leszczynski <leszczynscyATnospam.yahoo.com.nospam>
Date: Sun May 29 2005 - 16:29:06 CEST

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.
Received on Thu Sep 29 16:15:58 2005