Conversion of Event Log Error Script to Python 3

Posted on
Tue May 24, 2022 8:22 pm
GRWilde offline
User avatar
Posts: 173
Joined: Nov 15, 2005
Location: Los Angeles

Conversion of Event Log Error Script to Python 3

I am having difficulty converting the following Python 2 script to Python 3. This script finds all Event Log errors and emails the results. I have changed the print theBody to print(theBody) but then the following error occurs. Any ideas how to fix this script are appreciated.

"Script Error embedded script error:
Script Error 'ascii' codec can't decode byte 0xc2 in position 7915: ordinal not in range(128)
Script Error Exception Traceback (most recent call shown last):

embedded script, line 15, at top level
File '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/encodings/ascii.py', line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 7915: ordinal not in range(128)"

Code: Select all
import os
from datetime import date

baseDir = indigo.server.getInstallFolderPath()
tmpFileName = os.path.join(baseDir, 'Logs/tmp.txt')

fileDate = str(date.today())
eventFileName = os.path.join(baseDir, 'Logs/' + fileDate + ' Events.txt')

theBody = ''

with open(tmpFileName, 'a') as tmpFile:
    with open( eventFileName, 'r' ) as log:
        for line in log:
            if 'Error' in line:
               tmpFile.write(line)
               theBody += line

# theBody is the body of  an email which is sent next
print theBody

indigo.variable.updateValue(1155409051, value=theBody)

theSubject = "All Indigo Event Log Errors"

indigo.variable.updateValue(1695073597, value=theSubject)

George Wilde

Posted on
Tue May 24, 2022 8:58 pm
FlyingDiver offline
User avatar
Posts: 7215
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Conversion of Event Log Error Script to Python 3

Try this. In Python 3, strings are unicode, not bytes. But file IO is bytes. So you need to encode your string when you write it to a file.


Code: Select all
import os
from datetime import date

baseDir = indigo.server.getInstallFolderPath()
tmpFileName = os.path.join(baseDir, 'Logs/tmp.txt')

fileDate = str(date.today())
eventFileName = os.path.join(baseDir, 'Logs/' + fileDate + ' Events.txt')

theBody = ''

with open(tmpFileName, 'a') as tmpFile:
    with open( eventFileName, 'r' ) as log:
        for line in log:
            if 'Error' in line:
               tmpFile.write(line.encode('UTF-8'))
               theBody += line

# theBody is the body of  an email which is sent next
print theBody

indigo.variable.updateValue(1155409051, value=theBody)

theSubject = "All Indigo Event Log Errors"

indigo.variable.updateValue(1695073597, value=theSubject)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue May 24, 2022 9:02 pm
FlyingDiver offline
User avatar
Posts: 7215
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Conversion of Event Log Error Script to Python 3

It might also be the line before, where you're looking for a string in the line read from the file. You could try changing that line to:
Code: Select all
            if b'Error' in line:

Which forces that string to a bytestring, which is same type as the file input buffer (line).

I haven't done much File IO in Python3, so I'm not sure the best way to do this.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue May 24, 2022 9:03 pm
FlyingDiver offline
User avatar
Posts: 7215
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Conversion of Event Log Error Script to Python 3

See https://docs.python.org/3/tutorial/inpu ... ting-files

Adding the file encoding to the file open will handle most of the conversion by itself.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed May 25, 2022 3:07 am
GRWilde offline
User avatar
Posts: 173
Joined: Nov 15, 2005
Location: Los Angeles

Re: Conversion of Event Log Error Script to Python 3

Thanks for the suggestion. It solved my problem just by including the encoding in the open file statement, namely:

with open(tmpFileName, 'a', encoding="utf-8") as tmpFile:
with open( eventFileName, 'r', encoding="utf-8") as log:

I run this script every evening to receive an email showing all Indigo errors that occurred during the day.

George Wilde

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests