I use this piece of code in a program I wrote. I use it to get a recursive list of image files, the call pattern is something like re.compile(r'\.(bmp|jpg|png)$', re.IGNORECASE)
. I think you get the idea.
def getFiles(dirname, suffixPattern=None):
dirname=os.path.normpath(dirname)
retDirs, retFiles=[], []
for root, dirs, files in os.walk(dirname):
for i in dirs:
retDirs.append(os.path.join(root, i))
for i in files:
if suffixPattern is None or \
suffixPattern.search(i) is not None:
retFiles.append((root, i))
return (retDirs, retFiles)
After you have the list, it would be easy to apply a renaming rule. os.rename
is your friend, see http://docs.python.org/library/os.html.