os.rename() error no such file or folder exists
Question: I have been trying to create a python script that logs my data into a .dat
file called 'log.dat' and every minute, rename log.dat to something else and
just start writing the incoming log data to a new, empty log.dat file.

But however my os.rename line is creating a error and I have been trying to
debug it for so long but it is not helping. I keep getting the same error
saying

    
    
    Error : Traceback (most recent call last):rov sel.:0; homenet:0(-1); current net:0;
     File "tracer.py", line 56, in <module>
    main()
    File "tracer.py", line 44, in main
    os.rename("/home/debian/fname", "/home/debian/log-{}.dat".format(time.strftime("%y%m%d%H%M%S")))
    OSError: [Errno 2] No such file or directory
    

This is my code :

    
    
    from __future__ import print_function
    def main():
    
    
    #!/usr/bin/python
    # get lines of text from serial port, save them to a file
    
    
        import serial, io
        import time
        import os
    
        s = open('log.dat', 'w')
        log = time.strftime("%Y%m%d-%H%M%S")
        s = open(log + '.dat', 'w')
    
        delete = 'cat /dev/null > log.dat'
    
        addr  = '/dev/ttyACM0'  # serial port to read data from
        baud  = 9600            # baud rate for serial port
        fname = 'log.dat'   # log file to save data in
        fmode = 'a'             # log file mode = append
    
        with serial.Serial(addr,9600) as pt, open(fname,fmode) as outf:
            spb = io.TextIOWrapper(io.BufferedRWPair(pt,pt,1),
                encoding='ascii', errors='ignore', newline='\r',line_buffering=$
            spb.readline()
            while (1):
                now = time.time()
                with open(fname,fmode) as outf:
                    while (time.time() - now) < 60:
                          x = spb.readline()  # read one line of text from serial$
                          print (x,end='')    # echo line of text on-screen
                          outf.write(x)       # write line of text to file
                          outf.flush()        # make sure it actually gets written
                          os.rename("/home/debian/fname", "/home/debian/log-           
                                    {}.dat".format(time.strftime("%y%m%d%H%M%S")))
    
    if __name__ == '__main__':
    
    main()
    

I don't understand what am I doing wrong here. Any suggestion or help is
deeply appreciated.


Answer: try :

    
    
    os.rename("/home/debian/"+fname, "/home/debian/log-           
    {}.dat".format(time.strftime("%y%m%d%H%M%S")))
    

or:

    
    
    os.rename("/home/debian/log.dat", "/home/debian/log-                             
    {}.dat".format(time.strftime("%y%m%d%H%M%S")))
    

