0

So if i use

tail 'path'

to view last few lines of text file I get 9 lines of data in this format:

20-3-2015 16:7:13 6

I use

splitted = file_open(name).rstrip().split(" ");

where the file_open function is

def file_open(name):
    f_name = prefix + name;
    offs = -10;
    with open(f_name, 'r') as f: # Open file to read
        while True:
            f.seek(offs,2) # Jump to final line and go to point in line to begin
            lines = f.readlines();
            if len(lines) >= 2:
                    return lines[-1]
            offs *= 2;

This should open file, go to last line return the full last line and then split up the three columns.

Instead the value of splitted is

['\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00']

whereas it should obviously be the final line. I have been using this code perfectly fine but all of a sudden I am getting this issue.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Ross
  • 1
  • 1

1 Answers1

0

You should not use seek for text files and readlines with binary files. The behavior is not defined. readlines can use internal buffers. Some systems behave awkward, if you seek below 0 or larger than the file.

Daniel
  • 42,087
  • 4
  • 55
  • 81
  • Could you suggest another method of finding the last line line of a text file? It is either that or my other option is to just create another file with only 1 line of text. I want to read the line and display certain data on an LCD display – Ross Mar 21 '15 at 18:27