3

I want to check if a specific string has changed from values. The values always change over time. I only want to do an action if the string has changed. The string gets its value from a text file. So i want to know if the text file has changed. I don't know what the best way is to do this.

with open(settings.txt, 'r') as text_file:
        custom_string = text_file.read().split("\n")[1]

if #custom_string has changed
    print("string has changed to", custom_string)
Nafeez Quraishi
  • 5,380
  • 2
  • 27
  • 34
  • Do you really want to know if the string changed or only if the file changed? – DrEichenbach May 13 '20 at 13:54
  • 1
    If you only care whether the file has changed, likely the fastest way to do this is to simply use a hash function. See https://stackoverflow.com/questions/22058048/hashing-a-file-in-python on hashing files in python. This will be computationally simpler than checking characters in the string. – DerekG May 13 '20 at 13:55

3 Answers3

2
import time
file = open("PATH TO THE FILE", "r")
check_list = file.readlines()
actual_list = []
def check():
    global check_list, actual_list
    actual_list = file.readlines()
    for i in range(len(check_list)):
        if len(actual_list >= len(check_list):
            if check_list[i] == actual_list[i]:
                pass
            else:
                print("The file changed !")
                check_list = actual_list
                break
        else:
            if actual_list[i] == check_list[i]:
                pass
            else:
                print("The file changed !")
                check_list = actual_list
                break
while True:
    check()
    time.sleep(5)

This will check every 5 seconds if your file changed

Quantum Sushi
  • 504
  • 7
  • 23
1
class Watcher:
    """ A simple class, set to watch its variable. """
    def __init__(self, value):
        self.variable = value

    def set_value(self, new_value):
        if self.value != new_value:
            self.pre_change()
            self.variable = new_value
            self.post_change()

    def pre_change(self):
        # do stuff before variable is about to be changed

    def post_change(self):
        # do stuff right after variable has changed
Uday
  • 464
  • 3
  • 11
0

I've found the solution. I've made a simple code to check if the file has been changed.

import time

with open('name_of_.txt', 'r') as playingnow_text_file1:
    data1 = playingnow_text_file1.read().split("\n")[1]


def data1read():

    with open('name_of_file.txt', 'r') as playingnow_text_file1:
        global data1
        data1 = playingnow_text_file1.read().split("\n")[1]

def data2read():
    with open('name_of_file.txt', 'r') as playingnow_text_file2:
        data2 = playingnow_text_file2.read().split("\n")[1]
    print("activatie loop")
    if data1 != data2:
        print("data1 is not the same as data2 = file has been changed")
        data1read()

while True:
    data2read()
    time.sleep(2) #loop data2read every 2 seconds