File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Source:https://github.com/Show-Me-the-Code/show-me-the-code
2+ # Author:renzongxian
3+ # Date:2014-12-10
4+ # Python 3.4
5+
6+ """
7+
8+ 第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
9+
10+ """
11+
12+ import os
13+ import sys
14+
15+
16+ def code_lines (target_file ):
17+ # Declare returned values
18+ total_lines = 0
19+ empty_lines = 0
20+ comment_lines = 0
21+
22+ file_object = open (target_file , 'r' )
23+ for line in file_object :
24+ # Split the string
25+ word_list = line .split ()
26+ if word_list == []:
27+ empty_lines += 1
28+ elif word_list [0 ] == '#' :
29+ comment_lines += 1
30+ total_lines += 1
31+
32+ file_object .close ()
33+ return total_lines , empty_lines , comment_lines
34+
35+
36+ if __name__ == "__main__" :
37+ t_lines = 0
38+ e_lines = 0
39+ c_lines = 0
40+ if len (sys .argv ) <= 1 :
41+ print ("Need at least 1 parameter. Try to execute 'python 0007.py $dir_path'" )
42+ else :
43+ for dir_path in sys .argv [1 :]:
44+ for file_name in os .listdir (dir_path ):
45+ file_path = os .path .join (dir_path , file_name )
46+ t , e , c = code_lines (file_path )
47+ t_lines += t
48+ e_lines += e
49+ c_lines += c
50+ print ("Total lines: %s. Empty lines: %s. Comment Lines: %s." % (t_lines , e_lines , c_lines ))
You can’t perform that action at this time.
0 commit comments