Chinaunix首页 | 论坛 | 博客
  • 博客访问: 886624
  • 博文数量: 380
  • 博客积分: 3495
  • 博客等级: 中校
  • 技术积分: 3996
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-02 09:35
文章分类

全部博文(380)

文章存档

2015年(2)

2014年(5)

2013年(9)

2012年(9)

2011年(67)

2010年(103)

2009年(182)

2008年(3)

我的朋友

分类: Python/Ruby

2010-12-05 17:34:34

'''
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()

阅读(471) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~