-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeLinesCounter.py
69 lines (54 loc) · 1.46 KB
/
CodeLinesCounter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python
#--coding=UTF8--
import os, sys
f1 = file("countingreport.txt",'w')
extension_name = ['py', 'cpp', 'c','h','cc','java','js', 'sh']
#########################################################
extention_set = set()
for i in extension_name:
extention_set.add(i)
filecount = 0
dircount = 0
linecount = 0
def isCodingFile(filename):
ex_name = os.path.splitext(filename)
ex_name = ex_name[1].split('.')[-1]
if ex_name in extention_set:
return True
return False
def counting(cpwd):
global filecount, dircount, linecount
if os.path.isdir(cpwd) == True:
dircount += 1
for p in os.listdir(cpwd):
tmp_pwd = os.path.join(cpwd, p)
counting(tmp_pwd)
else:
if not isCodingFile(cpwd):
return
filecount += 1
f2 = file(cpwd)
tmpcount = 0
while 1:
line = f2.readline()
if len(line) == 0:
break
tmpcount += 1
f2.close()
linecount += tmpcount
f1.write(cpwd + " " + str(tmpcount)+'\n')
#f1.write(cpwd + " " + str(os.stat(cpwd)[0])+'\n')
if len(sys.argv) == 1:
print "Usage : $ python CodeLinesCounter.py file(directory)name1 file(directory)name1 ... "
input_name = sys.argv[1:]
input_list = ""
for name in input_name:
counting(name)
input_list = input_list + " " + name
print "######[Done]######"
print "Input File :" + input_list
print "Count of directory :" + str(dircount)
print "Count of file :" + str(filecount)
print "Count of line :" + str(linecount)
print "Refer to countingreport.txt for details."
f1.close()