File tree Expand file tree Collapse file tree 3 files changed +28
-5
lines changed
Expand file tree Collapse file tree 3 files changed +28
-5
lines changed Original file line number Diff line number Diff line change 77"""
88
99import os
10-
10+ import re
1111
1212def walk_dir (path ):
13- for root , dirs , files in os .walk (path ):
14- for f in files :
15- if f .lower ().endswith ('txt' ):
16-
13+ file_path = []
14+ for root , dirs , files in os .walk (path ):
15+ for f in files :
16+ if f .lower ().endswith ('txt' ):
17+ file_path .append (os .path .join (root , f ))
18+ return file_path
19+
20+ def find_key_word (filepath ):
21+ word_dic = {}
22+ filename = os .path .basename (filepath )
23+ with open (filepath ) as f :
24+ text = f .read ()
25+ words_list = re .findall (r'[A-Za-z]+' , text .lower ())
26+ for w in words_list :
27+ if w in word_dic :
28+ word_dic [w ] += 1
29+ else :
30+ word_dic [w ] = 1
31+ sorted_word_list = sorted (word_dic .items (), key = lambda d : d [1 ])
32+ print u"在%s文件中,%s为关键词,共出现了%s次" % (filename , sorted_word_list [- 1 ][0 ], sorted_word_list [- 1 ][1 ])
33+
34+ if __name__ == "__main__" :
35+ for file_path in walk_dir (os .getcwd ()):
36+ find_key_word (file_path )
37+
Original file line number Diff line number Diff line change 1+ a b c d a a d
Original file line number Diff line number Diff line change 1+ a b c d a a d d b d d x zx fd fd fd fd fd fd fd fd fd FD FD FD FD D FD FD FD FD fD FD FD FD
You can’t perform that action at this time.
0 commit comments