0

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() 
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147

2 Answers2

2

If the initial line has changed, set initial to current.
In the function checkvar()

def checkvar():
     initial = getvar()
     print("Initial var: {}".format(initial))
     while True:
        current = getvar()
        if initial == current:
            pass                                
        else:
            initial = current #initial line set to current line
            print("var has changed!")
            pass
  • This worked for me however, with the script running it is using over 30% of my cpu. Is there a way to resolve the issue. I thought maybe it's the way I'm reading the text so I tried to read it using first_line = open("readme.txt").readline().rstrip() but still the same issue. What's is causing the high cpu usage – Ken Robertson Nov 29 '22 at 19:55
  • @KenRobertson The high cpu usage is due to the fact that the file is scanned in every iteration of the while loop. You can slow down the while loop by including a sleep(5) statement (It will check the file after every 5 seconds). – Devansh_Jain_21 Nov 30 '22 at 04:03
2

You are never re-assigning initial, so when you check if initial == current, it's only checking against the very first version of the file. Instead, re-assign as follows

def getvar():
    with open('readme.txt') as f:
        first_line = f.readline().strip('\n')
    result = first_line
    return result

def checkvar():
    last = getvar()
    while True:
        current = getvar()
        if last != current:
            print("var has changed!")
            last = current

checkvar()
Justin Chang
  • 194
  • 12
  • This worked for me however, with the script running it is using over 30% of my cpu. Is there a way to resolve the issue. I thought maybe it's the way I'm reading the text so I tried to read it using first_line = open("readme.txt").readline().rstrip() but still the same issue. What's is causing the high cpu usage – Ken Robertson Nov 29 '22 at 19:55
  • @KenRobertson I would assume it's because of the infinite loop along with having to read in the entire file to check for changes. Check out this SO question for some pre-built ways you can accomplish file-watching in Python https://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-changes – Justin Chang Dec 01 '22 at 15:04