I'm trying to write a script that will check if the first line of a text file has changed and print the value once. It needs to be an infinite loop so It will always keep checking for a change. The problem I'm having is when the value is changed it will keep constantly printing and it does not detect the new change. What I need to is the script to constantly check the first line and print the value once if it changes and do nothing if it does not change. This is what I tried so far:
def getvar():
with open('readme.txt') as f:
first_line = f.readline().strip('\n')
result = first_line
return result
def checkvar():
initial = getvar()
print("Initial var: {}".format(initial))
while True:
current = getvar()
if initial == current:
pass
else:
print("var has changed!")
pass
checkvar()