Fix file read error

This commit is contained in:
Ruben 2018-01-04 21:18:32 +01:00
parent e2c8d5b3c3
commit 5aa4f65c9f
1 changed files with 25 additions and 11 deletions

View File

@ -54,22 +54,36 @@ lcd.message("Init scanner...")
prevFaceCount = 0
totalUse = 0 #in face-seconds
logfile = "/home/pi/scan_faces/scan_faces.log"
def tail(filepath):
"""
Thanks: https://stackoverflow.com/a/41491521
"""
with open(filepath, "rb") as f:
first = f.readline() # Read the first line.
f.seek(-2, 2) # Jump to the second last byte.
while f.read(1) != b"\n": # Until EOL is found...
try:
f.seek(-2, 1) # ...jump back the read byte plus one more.
except IOError:
f.seek(-1, 1)
if f.tell() == 0:
break
last = f.readline() # Read last line.
return last
# make sure log file exists
if not os.path.exists("scan_face.log")
with open("scan_face.log","w") as f:
if not os.path.exists(logfile):
with open(logfile,"w") as f:
f.write("{},{},{}".format(time.time(), 0,0))
# get last line of log file and update 'total use' using that.
with open("scan_face.log", "rb") as f:
first = f.readline() # Read the first line.
f.seek(-2, os.SEEK_END) # Jump to the second last byte.
while f.read(1) != b"\n": # Until EOL is found...
f.seek(-2, os.SEEK_CUR) # ...jump back the read byte plus one more.
last = f.readline() # Read last line.
bits = last.split(",")
totalUse = bits[2]
last = tail("")
bits = last.split(",")
totalUse = bits[2]
log = open("scan_face.log", "a")
log = open(logfile, "a")
lastFaceTime = datetime.datetime.utcnow()