Created
March 22, 2022 14:51
-
-
Save iuriguilherme/081942fbe1635f8e053a4cb7b69186d4 to your computer and use it in GitHub Desktop.
Monitor files being accessed recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import datetime, glob, os, sys | |
try: | |
## Default to pwd | |
root = './' | |
## For python files in the current folder (and subfolders recursively) this would be `filesystem_watch.py './' '/*.py'` | |
pattern = '' | |
if len(sys.argv) > 1: | |
root = str(sys.argv[1]) | |
if len(sys.argv) > 2: | |
pattern = str(sys.argv[2]) | |
print("Generating files list...") | |
files = [{'path': file_path, 'access': os.stat(file_path).st_atime, 'exists': True} for file_path in glob.glob(root + '/**' + pattern, recursive=True)] | |
print("ready.") | |
while True: | |
for file in files: | |
if file['exists']: | |
try: | |
access = os.stat(file['path']).st_atime | |
if access > file['access']: | |
print("{} accessed at {}".format(file['path'], str(datetime.datetime.fromtimestamp(access)))) | |
file['access'] = access | |
except FileNotFoundError: | |
file['exists'] = False | |
except KeyboardInterrupt: | |
print('done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment