path python module
I don’t think I’ve mentioned the path python module in detail before, so I will now.
It’s amazingly useful, and so much more intuitive than the standard library equivalents (os.path, etc.). If you’re like me, and you never got the hang of iteration or recursion in bash scripting, this module is for you.
I’m using it to iterate over all directories on the server with a ‘preproc.log’ file in them and collecting information about some of the files within.
import time
import path
DAY = 24 * 3600 # seconds
NOW = time.time()
globs = (("run*_raw+orig.BRIK", 21), # glob, max age
("p*_auto+tlrc.BRIK", 28))
def age(f):
return int(NOW - f.atime) / DAY
p = path.path("/home")
for preproc_file in p.walkfiles("preproc.log"):
p_dir = preproc_file.parent
for glob_pattern, max_age in globs:
for f in p_dir.files(glob_pattern):
if f.islink():
# ignore symlinks
continue
elif age(f) > max_age:
print f.size, f