Hacker News new | ask | show | jobs
by potash 4163 days ago
Note that your code still forces python to read the entire file! If all you want is the last line of a file, use the UNIX tail:

  tail -n 1 $f
Tail seeks backwards so it will only read the one line. Of course, this won't give you a line count.

EDIT: I haven't tested it but you might be interested in this implementation of tail in python: http://stackoverflow.com/a/136368

1 comments

I've done timing tests and it is typically on par with tail & wc. The bulk of the time is wasted reading your file into ram, the time it takes to count the lines is essentially 0.

Edit: Of course I misspoke, yes tail is much faster for getting the last line of the file! I meant for getting the line count the loop methods is typically just ~5% slower than wc on sufficiently large files.