There are lots of questions about using python to determine whether files were modified within a directory (see [1], [2]). Invariably the answer to these questions involves walking through the directory (os.walk
) to check things individually.
Is there any way to do this without a walk?
I have a large/deep directory structure and it is costly to check each subdirectory (and its subdirectories, recursively). I am wondering if this task can be accomplished only looking at a top level directory.
In this schema, the modification time of dir
changes when subdir
is created. But it does not change when file
is created. The problem would be solved if modification times were affected by all child files and directories.
dir/
|--- subdir/
│ |------- file
A quick script to facilitate testing:
import os
os.system('rm -rf dir')
os.system('mkdir dir')
m1 = os.path.getmtime('dir')
os.system('mkdir dir/subdir')
m2 = os.path.getmtime('dir')
os.system('touch dir/subdir/file')
m3 = os.path.getmtime('dir')
print m1 == m2 # False
print m2 == m3 # True