tags:

views:

182

answers:

6

Hi

Imagine I have a file with

Xpto,50,30,60 Xpto,a,v,c Xpto,1,9,0 Xpto,30,30,60

that txt file can be appended a lot of times and when I open the file I want only to get the values of the last line of the txt file... How can i do that on python? reading the last line?

Thanks

A: 

Not sure about a python specific implementation, but in a more language agnostic fashion, what you would want to do is skip (seek) to the end of the file, and then read each character in backwards order until you reach the line feed character that your file is using, usually a character with value 13. just read forward from that point to the end of the file, and you will have the last line in the file.

Kibbee
Reading one character at a time backwards from the end of the file is *really* expensive. Consider what you'd have to do to make that happen.
Dustin
Not sure how slow it would be in real life. The OS probably optimizes it anyway, when you fetch a single byte, it probably swaps a whole sector from the hard drive into memory. Can it even read less than a sector? Unless you last line was many sectors long, you probably wouldn't notice a slowdown.
Kibbee
+4  A: 

I think my answer from the last time this came up was sadly overlooked. :-)

If you're on a unix box, os.popen("tail -10 " + filepath).readlines() will probably be the fastest way. Otherwise, it depends on how robust you want it to be. The methods proposed so far will all fall down, one way or another. For robustness and speed in the most common case you probably want something like a logarithmic search: use file.seek to go to end of the file minus 1000 characters, read it in, check how many lines it contains, then to EOF minus 3000 characters, read in 2000 characters, count the lines, then EOF minus 7000, read in 4000 characters, count the lines, etc. until you have as many lines as you need. But if you know for sure that it's always going to be run on files with sensible line lengths, you may not need that.

You might also find some inspiration in the source code for the unix tail command.

fivebells
A: 

I prefer to treat file lines with

for Line in inFile:

 last_one = Line.split(",")

so there is any chance my for goes until the last Line and only split the last line of my file?

I need something very easy.

thanks

UcanDoIt
A: 

f.seek( pos ,2) seeks to 'pos' relative to the end of the file. try a reasonable value for pos then readlines() and get the last line.

You have to account for when 'pos' is not a good guess, i.e. suppose you choose 300, but the last line is 600 chars long! in that case, just try again with a reasonable guess, until you capture the entire line. (this worst case should be very rare)

hasen j
A: 

is your file really so huge that you can't just read the whole file and discard all but the last line? KISS

Dustin Getz