Bobys Bobys - 1 year ago 98
Python Question

Create log file one every day in python

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?

zmo zmo
Answer Source

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 = 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()

which will write into /var/log/gps:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download