Skip to content

Instantly share code, notes, and snippets.

@Mahrjose
Created January 25, 2022 06:09
Show Gist options
  • Save Mahrjose/3b7f693567dc43c05f9c8343f74dc348 to your computer and use it in GitHub Desktop.
Save Mahrjose/3b7f693567dc43c05f9c8343f74dc348 to your computer and use it in GitHub Desktop.
Track a file for changes and if file changes do something.
import os
class FileTracker(object):
def __init__(self, filename):
self._cached_stamp = 0
self.filename = filename
def is_modified(self):
stamp = os.stat(self.filename).st_mtime
if stamp != self._cached_stamp:
self._cached_stamp = stamp
return True
return False
def main():
# Path of the file you're tracking
FILE = "./path/to/file"
print("Tracking...")
test = FileTracker(FILE)
while True:
if test.is_modified():
# File you're tracking has changed, Do something...
pass
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment