File: How to read the last line? 
Author Message
 File: How to read the last line?


Quote:
> How can I jump to the last non-empty line in a openned file?
> Do I have to use readlines() and go to the last item in the
> list? What if it is a large text file?

Disclaimer: I'll be interested in seeing what someone who knows what they
are doing recommends,

I suspect if you don't want to eat the whole file, doing something like
seek(-256, 2), then a read(256)  and searching for the last line yourself
might work.



Sun, 21 Oct 2001 03:00:00 GMT  
 File: How to read the last line?
How can I jump to the last non-empty line in a openned file?
Do I have to use readlines() and go to the last item in the
list? What if it is a large text file?
Thanks.


Mon, 22 Oct 2001 03:00:00 GMT  
 File: How to read the last line?
A small variation on Bowman's idea. But a problem is what is the max length
of a line ? Working in SGML a line can be huge.

fp=open('xxx.sgm')
fp.seek(-1024,2)
l=fp.readlines()
print l[-1]

--Darrell



Mon, 22 Oct 2001 03:00:00 GMT  
 File: How to read the last line?

Quote:

> A small variation on Bowman's idea. But a problem is what is the max length
> of a line ? Working in SGML a line can be huge.

> fp=open('xxx.sgm')
> fp.seek(-1024,2)
> l=fp.readlines()
> print l[-1]

Have a look at Tools/scripts/rgrep.py in the python source
distribution; it's a reverse grep (i.e. it searches from the end of
the file backwards) that deals with this, including what to do if the
last line is really big.

--Guido van Rossum (home page: http://www.python.org/~guido/)



Mon, 22 Oct 2001 03:00:00 GMT  
 File: How to read the last line?


Quote:

>Have a look at Tools/scripts/rgrep.py in the Python source
>distribution; it's a reverse grep (i.e. it searches from the end of
>the file backwards) that deals with this, including what to do if the
>last line is really big.

I think you mean "in the 1.5.2 distribution"; I don't see it in 1.5.1.
--

Hugs and backrubs -- I break Rule 6       <*>       http://www.*-*-*.com/
Androgynous poly {*filter*} vanilla {*filter*} het

"In the end, outside of spy agencies, people are far too trusting and
willing to help."  -- Ira Winkler



Mon, 22 Oct 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. read the last line in file

2. How to read the x last lines of a text file

3. How to read the last line of http:// file

4. how to read text files line by line?

5. reading file line by line

6. new question in how can i read data from file line by line

7. line-by-line file read

8. Read a text file line by line

9. join last 2 lines of a file

10. deleting newline from last line in file

11. print last line of input file

12. how to specify last line of file ?

 

 
Powered by phpBB® Forum Software