0

Hello currently i am studying python and i wanted to know on how you can have a list that is being appended if there is a change constantly to a txtfile. Wording is a hard here is the code anyways

list=[]
random_number=0
file_handler=open("history.txt","w")
file_handler.write(str(list))
lenght_cumulative_data=len(list)
confirmed.append(random_number)

Now what i want to accomplish is that the list variable of the number 0 would be shown in history.txt but that doesnt happen and lets just imagine that random_number is always changing I want the list variable to be able to always update itself. Like if let say random_number changes to 1 and then 2 I want list to be updated to [0,1,2]. How do you do that? I've been searching on youtube and all they gave me is this write function is there anyway someone could refrence it or have any ideas?

2 Answers2

0
from os import stat
from _thread import start_new_thread
from time import sleep

List = []

class WatchFileForChanges:
    def __init__(self, filename):
        self.file = filename
        self.cached_file = stat(self.file).st_mtime

    def watch(self):
        num = 0
        while 1:
            status = stat(self.file).st_mtime
            if status != self.cached_file:
                self.cached_file = status
                #file changed
                List.append(num)
                num += 1

def main():

    Watcher = WatchFileForChanges("file.txt")

    start_new_thread(Watcher.watch, ())

    while 1:
        print(List)
        sleep(1)


if __name__ == '__main__':
    main()

This will do what you want. If I understood you correctly, you want to append to the list every time a file changes.

AM Z
  • 423
  • 2
  • 9
  • is there anyother way that is way more simpler or lets say i dont want it to be updated to the file just the list that is always updating to constant change. – Dwika Haryo Radithya Aug 26 '20 at 12:33
  • This code isn't updating the file, it appends a number to the list `List` whenever the file content is changed – AM Z Aug 26 '20 at 13:36
0

Note: this answer will only work on Windows

changes.py:

# Adapted from http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html

import threading
import os

import win32file
import win32con

ACTIONS = {
    1 : "Created",
    2 : "Deleted",
    3 : "Updated",
    4 : "Renamed from something",
    5 : "Renamed to something"
}
# Thanks to Claudio Grondi for the correct set of numbers
FILE_LIST_DIRECTORY = 0x0001

def monitor_changes(callback, path, filenames):
    path = path or ""
    if type(filenames) == "str":
        filenames = (filenames,)

    thread = threading.Thread(target=_monitor, args=(callback, path, filenames))
    thread.start()

    return thread

def _monitor(callback, path, filenames):
    hDir = win32file.CreateFile (
        path,
        FILE_LIST_DIRECTORY,
        win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
        None,
        win32con.OPEN_EXISTING,
        win32con.FILE_FLAG_BACKUP_SEMANTICS,
        None
    )
    while True:
        #
        # ReadDirectoryChangesW takes a previously-created
        # handle to a directory, a buffer size for results,
        # a flag to indicate whether to watch subtrees and
        # a filter of what changes to notify.
        #
        # NB Tim Juchcinski reports that he needed to up
        # the buffer size to be sure of picking up all
        # events when a large number of files were
        # deleted at once.
        #
        results = win32file.ReadDirectoryChangesW (
            hDir,
            1024,
            True,
            win32con.FILE_NOTIFY_CHANGE_LAST_WRITE,
            None,
            None
        )

        for action, file in results:
            if filenames and file not in filenames and os.path.basename(file) not in filenames:
                continue
            callback(action, file)

if __name__ == '__main__':
    # monitor by printing
    t = monitor_changes(print, ".", None)

And in your main.py:

import changes
import os

my_list = []

def callback(action_id, filename):
    # the function running means
    # that the file has been modified
    
    action_desc = changes.ACTIONS[action_id]
    print(action_desc, filename)

    with open(filename) as f:
        my_list.append(f.read())

thread = changes.monitor_changes(callback, ".", "my_file_that_I_want_to_monitor.txt")

If you want to monitor all files in the directory, call monitor_changes with None as the third argument.

Note: this will monitor all subdirectories, so files with the same name but in different folders will trigger the callback. If you want to avoid this, then check the filename passed to your callback function is exactly what you want to monitor.

Ed Ward
  • 2,333
  • 2
  • 10
  • 16