How do i read just the last line of a text file?
Chris F.A. Johnson
cfajohnson at gmail.com
Sun May 29 03:40:57 EDT 2005
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 <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
More information about the Python-list
mailing list