Python update last line of file (stdout)
Question: In Python program, I am redirecting `stdout` using below:

    
    
    sys.stdout = open("log_file.txt", "a",0)
    

On certain condition I want rewrite the last line of the file.

I have tried below:

    
    
    if (status=='SAME'):
      print '\r'+'Above status doesnot change and last checked @'+str(datetime.datetime.fromtimestamp(time.time())),
    

This seems to work when I look at file using tail command.

    
    
    tail -f log_file.txt
    

However when the I look at the original content of the file, it is not
overwriting the last line but it is appending.

Please suggest me any other approaches keeping `sys.stdout =
open("log_file.txt", "a",0)` as is.

My code is Producing output as :

    
    
    0007505-160614083053377-oozie-oozi-W-> Started   : 2016-06-14 16:15:32
    0007505-160614083053377-oozie-oozi-W@HiveScript                               RUNNING   job_1465907358342_1346 RUNNING    -
    Above status doesnot change and last checked @2016-06-14 16:15:43.096288 
    Above status doesnot change and last checked @2016-06-14 16:15:53.344065 
    Above status doesnot change and last checked @2016-06-14 16:16:03.672789 
    0007505-160614083053377-oozie-oozi-W@end                                      OK        -                      OK         -
    

I want it to be

    
    
     0007505-160614083053377-oozie-oozi-W-> Started   : 2016-06-14 16:15:32
     0007505-160614083053377-oozie-oozi-W@HiveScript                               RUNNING   job_1465907358342_1346 RUNNING    - 
     Above status doesnot change and last checked @2016-06-14 16:16:03.672789 
     0007505-160614083053377-oozie-oozi-W@end                                      OK        -                      OK         -
    


Answer: Without actually running a test case, I'd try:

  * `open` the file in `append` mode
  * read lines, noting the file position at the start of each line
  * after reading the last line, use `seek` to move back to the start of that line
  * `write` the new text (make sure it is at least as long as the original).
  * `close` the file

===================

    
    
    from __future__ import print_function  # for 2.7
    f = open('log.txt', mode='r+')   # for preexisting file
    for i in range(10):
        ref = f.tell()
        print('%s line %s'%(i,ref), file=f)
        if (i % 3)==0:
            f.seek(ref)
            print('%s oops %s'%(i,ref), file=f)
    ref = f.tell()
    print('ending at %3d'%100, file=f)
    f.seek(ref)
    print('ending at %3d'%f.tell(), file=f)
    f.close()
    

produces:

    
    
    2200:~/mypy$ cat log.txt
    0 oops 0
    1 line 9
    2 line 18
    3 oops 28
    4 line 38
    5 line 48
    6 oops 58
    7 line 68
    8 line 78
    9 oops 88
    ending at  98
    

In 2.7 this form also works:

    
    
    sys.stdout = f
    for i in range(10):
        ref = f.tell()     # or ref = sys.stdout.tell()
        print '%s line %s'%(i,ref)
        if (i % 3)==0:
            f.seek(ref)
            print '%s oops %s'%(i,ref)
    

