Fix file read error
This commit is contained in:
parent
e2c8d5b3c3
commit
5aa4f65c9f
1 changed files with 25 additions and 11 deletions
|
@ -54,22 +54,36 @@ lcd.message("Init scanner...")
|
||||||
prevFaceCount = 0
|
prevFaceCount = 0
|
||||||
totalUse = 0 #in face-seconds
|
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
|
# make sure log file exists
|
||||||
if not os.path.exists("scan_face.log")
|
if not os.path.exists(logfile):
|
||||||
with open("scan_face.log","w") as f:
|
with open(logfile,"w") as f:
|
||||||
f.write("{},{},{}".format(time.time(), 0,0))
|
f.write("{},{},{}".format(time.time(), 0,0))
|
||||||
|
|
||||||
# get last line of log file and update 'total use' using that.
|
# get last line of log file and update 'total use' using that.
|
||||||
with open("scan_face.log", "rb") as f:
|
last = tail("")
|
||||||
first = f.readline() # Read the first line.
|
bits = last.split(",")
|
||||||
f.seek(-2, os.SEEK_END) # Jump to the second last byte.
|
totalUse = bits[2]
|
||||||
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]
|
|
||||||
|
|
||||||
log = open("scan_face.log", "a")
|
log = open(logfile, "a")
|
||||||
|
|
||||||
lastFaceTime = datetime.datetime.utcnow()
|
lastFaceTime = datetime.datetime.utcnow()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue