Recursive Directory Contents

Mark Pilgrim f8dy at yahoo.com
Mon Feb 26 17:24:48 CET 2001


The os.path module has a wonderful method called "walk", which takes a root
directory, a function, and an arbitrary argument.  It recursively walks
through the root directory, calling the function with 3 parameters: <your
arbitrary argument>, <current directory>, <list of files in current
directory>.  For example, this script will find all the files anywhere
within the "/music" directory that have been modified since Feb 15, 2001,
store them (with fully qualified paths) in modifiedFilesWithPaths, and then
print them.

-----

import os
import time

def mtimeChecker(arg, dirname, fileList):
  (mtime, modifiedList) = arg
  for f in [os.path.join(dirname, file) for file in fileList]:
    if os.path.getmtime(f) > mtime:
      modifiedList.append(f)

modifiedFilesWithPaths = []
os.path.walk('/music', mtimeChecker, \
  (time.mktime((2001, 2, 15, 0, 0, 0, 0, 0, 0)), \
  modifiedFilesWithPaths))
for f in modifiedFilesWithPaths:
  print f

-----

-M
You're smart; why haven't you learned Python yet?
http://diveintopython.org/

"C. Porter Bassett" <porter at et.byu.edu> wrote in message
news:mailman.983161805.496.python-list at python.org...
> Is there a common module out there that will recursively record the
> contents of a directory?  I want to use it to do tests to see what files
> have been modified since last check.
>
>
>
>





More information about the Python-list mailing list