How to log and save file with date and timestamp in Python

10,726

Solution 1

In Case 2, every time write_temp is called, it is populating filename1 with timestamp.

So consider,for example, you called it at 10:15:13 (hh:mm:ss), then filename1 will be 10-15-13.csv. When you will call it again at 10:15:14 then filename1 will be 10-15-14.csv.

That's why new file is getting created.

Solution : Take out filename1 from temp_write and pass filename to that function as argument.

from datetime import *
import sys

def write_temp(temperature,file_name):

        print ("In write_temp function - "+file_name)

        with open(file_name, 'a') as log:
                log.write("{0},{1}\n".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"),str(temperature)))


arg = sys.argv[1]
filename1 = str(arg) + "-" + datetime.now().strftime("%Y-%m-%d-%H-%M-%S")+".csv"
print ("File name is "+filename1)
write_temp(1,filename1)

Output on console:

C:\Users\dinesh_pundkar\Desktop>python c.py LOG
File name is LOG-2016-09-27-11-03-16.csv
In write_temp function - LOG-2016-09-27-11-03-16.csv

C:\Users\dinesh_pundkar\Desktop>

Output of LOG-TimeStamp.csv:

2016-09-27 10:47:06,1
2016-09-27 10:47:06,3

Solution 2

Try :

with open('Log-%s.csv' % filename1, 'a') as log:
Share:
10,726
Dr.Viper
Author by

Dr.Viper

BY DAY: Research Engineer (Master's in Materials Science and Engineering, Bachelor's in Mechanical Engineering) BY NIGHT: Coding Ninja (Wannabe). Learning C#, JAVA and PYTHON. One too many.

Updated on June 05, 2022

Comments

  • Dr.Viper
    Dr.Viper almost 2 years

    I am trying to log temperature from a DS18B20 sensor using Raspberry Pi 3 via Python code executed from shell.

    I want to log temperature with timestamp and then save the file.

    What I am doing presently is saving it to a filename entered in the code, but I want to log the file with date and timestamp in filename.

    Case 1 : When I put a filename in the code, then I can append data to the same file over and over, but I can't start a new separate logging without editing the code.

    #Writes data to file
    def write_temp(temperature):
            with open("/home/pi/temp.csv", "a") as log:
                    log.write("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),str(temperature)))
    

    Problem is that the file is always temp.csv and data gets appended each time.

    Case 2: I tried to get filename from timestamp, but each second a new file is getting generated.

    def write_temp(temperature):
            filename1 = strftime("%Y-%m-%d %H:%M:%S")
            #filename1 = sys.argv[1]
            with open('%s.csv' % filename1, 'a') as log:
                    log.write("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),str(temperature)))
    

    In the above case, I would rather like to have the filename set at the start of logging each time or at the end of logging. I would also like to save the name as Log-DateTime instead of just DateTime. I tried this by doing ('"Log-" + %s.csv' % filename1, 'a') instead of ('%s.csv' % filename1, 'a'), but that didn't work out.

    Ideal Case: I want file name to be WORD-DateTime, where WORD is sent as an argument from the command line, as in below:

    sudo python TTLogging.py WORD
    

    Can you point out where I am going wrong? I can share the full code of my work if required since it is a learning exercise.

  • Dr.Viper
    Dr.Viper over 7 years
    Thanks for the input, I tried, and the problem of getting a new file each second of logging remains. I am getting Log-date and time.csv, Log-date and time after 1s.csv, etc..... I want to log to 1 file till the logging is stopped. even though a is there, it is not appending.
  • Dr.Viper
    Dr.Viper over 7 years
    Thank you for the input, what is the significance of write_temp(1) and write_temp(3)?
  • Dinesh Pundkar
    Dinesh Pundkar over 7 years
    @Dr.Viper - Please check the updated code. I have just called the above function to test it
  • Dr.Viper
    Dr.Viper over 7 years
    Ok, now that makes sense to me. Also, shouldn't the line ¬filename1 = str(arg) + "-" + datetime.now().strftime("%Y-%m-%d-%H-%M-%S")+".csv"¬ be file_name =
  • Dinesh Pundkar
    Dinesh Pundkar over 7 years
    Not required. file_name is function argument where as filename1 is variable defined in main block. Both variables are in kind of different scope.
  • Dr.Viper
    Dr.Viper over 7 years
    Then sorry for making the edit to your answer. I apologize. But how will the function write_temp know that it has to use filename1 for file_name? Genuine question. Not a CS fellow here, unfortunately. Sorry to bother.
  • Dinesh Pundkar
    Dinesh Pundkar over 7 years
    So, when I am calling write_temp(1,filename1) I am passing 1 and filename1. In function write_temp temperature is populated with 1 and file_name is populated with filename1
  • Dinesh Pundkar
    Dinesh Pundkar over 7 years
    So now function write_temp knows which file to write.
  • Dinesh Pundkar
    Dinesh Pundkar over 7 years
    So for your understanding, add print (filename1) after filename1 = str(arg) + "-" + datetime.now().strftime("%Y-%m-%d-%H-%M-%S")+".csv"
  • Dinesh Pundkar
    Dinesh Pundkar over 7 years
    Add print ("In function "+file_name) in write_temp after def write_temp(temperature,file_name):
  • Dinesh Pundkar
    Dinesh Pundkar over 7 years
    @Dr.Viper - Check my updated code and output. I have printed filename from both place
  • Dr.Viper
    Dr.Viper over 7 years
    I am getting list index out of range for the arg = sys.srgv[1] command
  • Dinesh Pundkar
    Dinesh Pundkar over 7 years
    Are you executing script as - python TTLogging.py WORD ??
  • Dr.Viper
    Dr.Viper over 7 years
    Nope. That was my mistake, also, next I found that I hadn't update write_temp(temperature) to write_temp(temperature,filename1). Now it works with filename1 as the argument for filename. I have learned more in Python from the last 30 minutes with you than in my attempts at it in the last 2 weeks. You are a genius. Thanks a million. How do I give you 100000 rep points for this?