I would like to write a script in python which will logging a file, one per day. It will log gps records and the log file will be a gpx(I have the script for that). So for today it will create a file named 12-05-2014.gpx and will keep all the gps records until it turns 13-05-2014. Then it will create a new file 13-05-2014.gpx and keep logging in there. Is this possible? Could you give me some hints about that please?
4 Answers
N.B.: I'm understanding that you're extending an existing python script that handles your GPS logs. If not, @aj8uppal may be right.
I suggest you should use the logging module to output your data, and take advantage of the RotatingFileHandler which will do that on your behalf. Along with the Formatter class, you can use the power logging module at your advantage for doing a rotating log.
Though if you consider the logging module of python is a no go – though I think that's the best option, you can always use the following write function in your program:
import os
import time
class RotatingFileOpener():
def __init__(self, path, mode='a', prepend="", append=""):
if not os.path.isdir(path):
raise FileNotFoundError("Can't open directory '{}' for data output.".format(path))
self._path = path
self._prepend = prepend
self._append = append
self._mode = mode
self._day = time.localtime().tm_mday
def __enter__(self):
self._filename = self._format_filename()
self._file = open(self._filename, self._mode)
return self
def __exit__(self, *args):
return getattr(self._file, '__exit__')(*args)
def _day_changed(self):
return self._day != time.localtime().tm_mday
def _format_filename(self):
return os.path.join(self._path, "{}{}{}".format(self._prepend, time.strftime("%Y%m%d"), self._append))
def write(self, *args):
if self._day_changed():
self._file.close()
self._file = open(self._format_filename())
return getattr(self._file, 'write')(*args)
def __getattr__(self, attr):
return getattr(self._file, attr)
def __iter__(self):
return iter(self._file)
which you can use as follows:
with RotateFileOpener('/var/log/gps', prepend='gps_data-', append='.gpx') as logger:
while True:
log = get_gpx_data()
logger.write(log)
which will write into /var/log/gps:
/var/log/gps/gps_data-20140512.gpx
/var/log/gps/gps_data-20140513.gpx
/var/log/gps/gps_data-20140514.gpx
…
2 Comments
while True: is for?while true is to create an infinite loop that will call the function that generates output to be logged, and then write it in the gpx log file, the log object taking care automagically to rotate the log files.Probably something along the lines of this:
import time
datafile = open('mydatafile')
while True:
filename = str(time.strftime("%d-%m-%Y"))+'.gpx'
with open(filename, 'w') as myfile:
myfile.write(datafile.read())
time.sleep(60)
Get the time using the time module, and use that as the filename. Then, every minute, you update that filename, and write the contents of datafile to that. Of course, you might have to change this code around because everything will not be the same for you.
Comments
Addition to the presented code:
File needs to be opened in append mode. On Windows and Rapsbian this did not function and I had to change this line: self._file = open(self._filename)
into: self._file = open(self._filename, self._mode)
Hope this will help someone to make full use of this.
GWS