432qw 发表于 2016-8-15 11:19:20

python实现linux下指定目录下文件中的单词个数统计

本帖最后由 432qw 于 2016-8-15 11:20 编辑




# !/usr/bin/env python
# -*- coding:utf-8 -*-
"""
    实现指定目录下的文件单词计数统计:
    例如:你输入
    nginx.conf(将要统计的文件)--->/usr(nginx.conf的搜索目录)--->输入要得到该文件的前top个单词数目
    结果: 得到---> (单词名,该文件的中该单词出现的次数)
"""
import os
import sys
import re

def 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 = for file in files]
      return files1

def 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 = )]
            wordcounts = sorted(wordcounts,key=lambda d: d, reverse=True)
            # get the right word format
            wordcounts = )]
         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)




页: [1]
查看完整版本: python实现linux下指定目录下文件中的单词个数统计