1 from os import path
2 from sys import argv
3 from os import listdir
4
5 totalLine=0
6
7 def countFile(fileName):
8 fd=open(fileName)
9 lines=fd.readlines()
10 return len(lines)
11
12 def isSource(fileName):
13 sourceList=('py', 'c', 'cpp', 'h')
14 tmp=fileName.split('.')
15 tailName=tmp[-1]
16 if tailName in sourceList:
17 return True
18 else:
19 return False
20
21 def countDir(cur_path):
22 file_list=listdir(cur_path)
23 global totalLine
24 for file_iter in file_list:
25 file_name=cur_path+'/'+file_iter
26 if path.isdir(file_name) :
27 countDir(file_name)
28 elif(isSource(file_name)):
29 totalLine+=countFile(file_name)
30 else:
31 pass
32
33 def main():
34 cur_path=argv[1]
35 countDir(cur_path)
36 print 'The project %s has %d lines' % (cur_path, totalLine)
37
38 main()