How do i read just the last line of a text file?
Chris F.A. Johnson
cfajohnson at gmail.com
Sun May 29 01:36:09 EDT 2005
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 <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