# !/usr/bin/env python# -*- coding:utf-8 -*-"""	实现指定目录下的文件单词计数统计:	例如:你输入	nginx.conf(将要统计的文件)--->/usr(nginx.conf的搜索目录)--->输入要得到该文件的前top个单词数目	结果: 得到  ---> (单词名,该文件的中该单词出现的次数)"""import osimport sysimport redef getfiles():    file_name = raw_input("please enter the file_name you want count the file\n")    path = raw_input("please enter the root path : example: /tmp\n")    if not os.path.exists(path):        print "sorry,the path %s is not exists !!" %(path)        sys.exit(1)    else:        cmd = "find " + path + " -name "+ file_name        print cmd        files = os.popen(cmd).readlines()        files1 = [file[:-1] for file in files]        return files1def countword(top_num):    files = getfiles()    for file in files:        with open(file) as f:            # get the file content and strip the space and line break(acording the os.linesep)            file_content = f.read().replace(os.linesep,"").replace("#","").replace("/","").replace(";","").replace("}","").replace("{","")            file_words = file_content.split(" ")            wordcounts = [(word, file_words.count(word)) for word in file_words]            # get uniq wordcounts            wordcounts = list(set(wordcounts))            # get the max wordcounts            # wordcounts = [wordc for wordc in wordcounts if re.match("^/s*",wordc[0])]            wordcounts = sorted(wordcounts,key=lambda d: d[1], reverse=True)            # get the right word format            wordcounts = [wordcount for wordcount in wordcounts if re.match("\w",wordcount[0])]     	    if len(wordcounts[:int(top_num)]) > 0: 	        print  "文件:" + f.name + "的前" + top_num + "个单词是及个数分别是:\n" + str(wordcounts[:int(top_num)])  	    else:	        print  "文件:" + f.name + "是个空文件!!\n" + str(wordcounts[:int(top_num)]) 	    	if __name__ == "__main__":    top_num = raw_input("请输入要得到统计单词数目的前多少个\n")    countword(top_num)