Dr.Viper Dr.Viper - 1 year ago 204
Python Question

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

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.

Answer Source

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)

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


Output of LOG-TimeStamp.csv:

2016-09-27 10:47:06,1
2016-09-27 10:47:06,3
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download