-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode-line-counter.py
50 lines (45 loc) · 944 Bytes
/
code-line-counter.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
import os
import sys
from sys import stdout
rootdir = sys.argv[1]
extDic = {}
def countLines(fPath):
count = 0
with open(fPath) as f:
for i, l in enumerate(f):
pass
count = i
return count + 1
def outTab(text):
padSize = 10
padChar = " "
if len(str(text)) >= 10:
stdout.write(text)
else:
pad = ""
for p in xrange(padSize - len(str(text))):
pad += padChar
stdout.write(str(text) + pad)
for root, subdirs, files in os.walk(rootdir):
for file in files:
fPath = root + "\\" + file;
fName, fExt = os.path.splitext(fPath)
if fExt in extDic.keys():
extDic[fExt] += countLines(fPath)
else:
extDic[fExt] = countLines(fPath)
outTab('FILE TYPE')
stdout.write(' | ')
outTab('LINE COUNT')
stdout.write('\n')
total = 0
for ext, lCount in extDic.iteritems():
outTab(ext)
stdout.write(' | ')
outTab(lCount)
stdout.write('\n')
total += lCount
outTab('TOTAL')
stdout.write(' | ')
outTab(total)
stdout.write('\n')