is there a better way to walk a file system?

ina erinhouston at gmail.com
Fri Jul 1 11:05:35 EDT 2005


I want to walk a folder structor and group all the files by extention.


Here is the code I put together is there a better way of doing this?
<code>
import os
folderKey = "Folders"
dicExt = {}
tDicKey = []
tDicKey.append(folderKey)
dicExt[folderKey] = []

walkPath = r"\\zek\C$\AST"

for d in os.walk(walkPath):
    (dirpath, dirnames, filenames) = d
    dicExt[folderKey].append(dirpath)
    for fname in filenames:
        fs = os.path.splitext(fname)
        if dicExt.has_key(fs[-1]):
            dicExt[fs[-1]].append(os.path.join(dirpath,fname))
        else:
            dicExt[fs[-1]] = []
            dicExt[fs[-1]].append(fname)
            tDicKey.append(fs[-1])


for k in tDicKey:
    print k +" "+ str(len(dicExt[k]))
</code>
Right now it is just prints out the counts.

Thanks 

Erin




More information about the Python-list mailing list