How do i read just the last line of a text file?
Andy Leszczynski
leszczynscyATnospam.yahoo.com.nospam
Sun May 29 10:29:06 EDT 2005
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.
More information about the Python-list
mailing list