'''
Created on 2010-12-5
@author: zhangyadong
'''
import os, sys
from stat import *
import io
def walktree(top, callback, fileMap):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file'''
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.lstat(pathname)[ST_MODE]
if S_ISDIR(mode):
# It's a directory, recurse into it
walktree(pathname, callback, fileMap)
elif S_ISREG(mode):
# It's a file, call the callback function
callback(pathname, fileMap)
else:
pass
# Unknown file type, print a message
# print 'Skipping %s' % pathname
def visitfile(file, fileMap):
fileName = os.path.split(file)[1]
if fileName in fileMap:
fileMap[fileName].append(file)
else:
fileMap[fileName]=[file]
#print 'visiting', file
if __name__ == '__main__':
fileMap = {}
walktree("/Users/zhangyadong", visitfile, fileMap)
print("the total is {0}".format(len(fileMap)))
f=open("/Users/zhangyadong/1.txt", "w")
for tmpArray in fileMap:
if isinstance(tmpArray,list) and len(tmpArray) > 1:
f.write(tmpArray)
f.write("\n")
f.close()
阅读(501) | 评论(0) | 转发(0) |