How to recall previous output of python script

527 views Asked by At

i am working on a telethon script of python which runs if the channel/group receives new message i am looking at the message id for running my script i am a beginner of python so with what knowledge i have

i am using this following code.

prev_msgid=0

latest_msgid = message.id

if  latest_msgid>prev_msgid:
    print('latest message')
    prev_msgid = message.id
else:
    print('old message') 

but when i run this code every time the previous message resets to 0

i need a way for when i run this code multiple times the prev_msgid is automatically changed to the latest message id.

thank you.

1

There are 1 answers

0
jacob galam On

like @Quba said you need a way to store data in persistent way

Pickle is the fastest solution for you. It can save python object as a file:

import pickle
from os import path

prev_msgid = 0

# check if saved
if path.exists("prev_msgid"):
    # load
    with open("prev_msgid", 'rb') as f:
        prev_msgid = pickle.load(f)

prev_msgid += 1

# save
with open("prev_msgid", 'wb') as f:
        pickle.dump(prev_msgid, f)

print(prev_msgid)

Every time you run the script it will add one to prev_msgid. See that it makes a file that named "prev_msgid"