Pretty simple. You're opening the file with mode 'ab', which means for writing, appending to the file, and in binary mode. Then you try to read it. Instead, open it in read mode:
with open(self.filename, 'rb') as f: Incidentally, this is a good example of a 'try' clause being too broad. You're catching everything, and not giving yourself enough information to diagnose the problem. Instead, try something like try: ... except FileNotFoundError as e: syslog.syslog(syslog.LOG_INFO, "File not found: %s" % e) except ValueError as e: syslog.syslog(syslog.LOG_INFO, "Could not convert string to value: %s" % e) etc. Or, even better, use multiple 'try' clauses. Finally, WeeWX V4 no longer uses syslog. Instead, it uses the module logging. See the wiki article *WeeWX V4 and logging <https://github.com/weewx/weewx/wiki/WeeWX-v4-and-logging>*. -tk On Sun, Nov 8, 2020 at 4:34 AM vigilance wx <[email protected]> wrote: > HI > I have a service running under weewx 3,9 its working fine connected to a > Davis vantage. On a second pi i have installed weewx 4.2 with view to > upgrading the 3.9 pi to the latest weewx version > the service i have on the weewx3.9 will not run under python 3 > > import syslog > import weewx > import os > import csv > from weewx.wxengine import StdService > class PlanetService(StdService): > def __init__(self, engine, config_dict): > super(PlanetService, self).__init__(engine, config_dict) > d = config_dict.get('PlanetService', {}) > self.filename = d.get('filename', '/home/pi/cc/allplanets.csv') > syslog.syslog(syslog.LOG_INFO, "planet: using %s" % self.filename) > self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_file) > > def read_file(self, event): > try: > with open(self.filename) as f: > f.seek(-10, os.SEEK_END) > line = f.readlines()[-1] > value = line.split(',') > > syslog.syslog(syslog.LOG_DEBUG, "allplanets: found value of > %s" % value) > event.record['plutoAzi'] = float(value[3]) > except Exception as e: > syslog.syslog(syslog.LOG_ERR, "allplanets: cannot read value: > %s" % e) > > the above runs under weewx 3.9 but it will not run under weewx 4.2 > > i have tried to modify if but i am getting the error in the log file > "/weewxd: allplanets: cannot read value: read" > > import syslog > import weewx > import os > import csv > > from weewx.wxengine import StdService > > class PlanetService(StdService): > def __init__(self, engine, config_dict): > super(PlanetService, self).__init__(engine, config_dict) > d = config_dict.get('PlanetService', {}) > self.filename = d.get('filename', '/home/pi/cc/allplanets.csv') > syslog.syslog(syslog.LOG_INFO, "planet: using %s" % self.filename) > self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_file) > > def read_file(self, event): > try: > with open(self.filename , 'ab') as f: > > f.seek(-10, 2) > line = f.readlines()[-1] > value = line.split(',') > > syslog.syslog(syslog.LOG_DEBUG, "allplanets: found value of > %s" % value) > event.record['extraTemp1'] = float(value[1]) > except Exception as e: > syslog.syslog(syslog.LOG_ERR, "allplanets: cannot read value: > %s" % e) > > could any one offer advice as to why it will not run? is it the seek > function? > > thanks for any advice > > -- > You received this message because you are subscribed to the Google Groups > "weewx-user" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/weewx-user/52382be8-9031-4d4a-85da-9353aecf40f8n%40googlegroups.com > <https://groups.google.com/d/msgid/weewx-user/52382be8-9031-4d4a-85da-9353aecf40f8n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "weewx-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/weewx-user/CAPq0zECmKnsqk7sTePAf1XPqG-fN5%2B-Rkk_MfzEvWwktF7CJ0A%40mail.gmail.com.
