Skip to content

Instantly share code, notes, and snippets.

@iuriguilherme
Created March 22, 2022 14:51
Show Gist options
  • Save iuriguilherme/081942fbe1635f8e053a4cb7b69186d4 to your computer and use it in GitHub Desktop.
Save iuriguilherme/081942fbe1635f8e053a4cb7b69186d4 to your computer and use it in GitHub Desktop.
Monitor files being accessed recursively
#!/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