keep track of file size

This commit is contained in:
Evgeny Zinoviev 2021-04-15 16:47:36 +03:00
parent dcf2cbdbee
commit 1e5a2df1a0

13
main.py
View File

@ -10,7 +10,7 @@ from argparse import ArgumentParser
def readstate(file: str) -> dict: def readstate(file: str) -> dict:
if not os.path.exists(file): if not os.path.exists(file):
default_state = {'seek': 0} default_state = {'seek': 0, 'size': 0}
writestate(file, default_state) writestate(file, default_state)
return default_state return default_state
@ -41,15 +41,22 @@ def main():
# read file # read file
state = readstate(args.state_file) state = readstate(args.state_file)
fsize = os.path.getsize(args.log_file)
if fsize < state['size']:
state['seek'] = 0
with open(args.log_file, 'r') as f: with open(args.log_file, 'r') as f:
# jump to the latest readed position if state['seek']:
f.seek(state['seek']) # jump to the latest readed position
f.seek(state['seek'])
# read till the end of the file # read till the end of the file
content = f.read() content = f.read()
# save new position # save new position
state['seek'] = f.tell() state['seek'] = f.tell()
state['size'] = fsize
writestate(args.state_file, state) writestate(args.state_file, state)
# if got something, send it to telegram # if got something, send it to telegram