I am wondering if there is a simple way to get to the penultimate line of an open file. f.seek is giving me no end of trouble. I can easily get to the final line, but I can't figure out how to get to the line above that.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Assuming the file isn't too big and memory isn't a concern
open('file.txt').readlines()[-2]
You can seek from the end of the file and count number of newlines encountered, as soon as you hit the second
'\n'
stop and callreadline()
:For a file like:
The output will be:
Files are a single long string of bytes on most systems (some have forks, extents or records), leaving the concept of lines to a higher level. Complicating matters further, the line ending doesn't look the same way on all platforms. This means you have to read the lines to identify them, and specifically for text files you can only seek() to places you found using tell().
If we're just reading the penultimate line, it's simple:
That approach loads the entire file into memory. If we want to replace the end of the file, starting with the penultimate line, things get hairier:
If you merely want to read lines in an arbitrary order, linecache might be helpful.
Each of these scans the entire file. Tools like tail may make another optimization: read data near the end of the file, until you've found enough newlines to identify the lines you need. This gets more complicated beause the seeking only works predictably in binary mode but the line parsing only works predictably in text mode. That in turn means our guess that the file is separated by linesep could be wrong; Python's universal newline support only operates in text mode.