-
Notifications
You must be signed in to change notification settings - Fork 1
/
CodeLineCounter.py
75 lines (61 loc) · 2.33 KB
/
CodeLineCounter.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
70
71
72
73
74
75
# > lines_of_code_counter.py .h .cpp
# Total lines: 15378
# Blank lines: 2945
# Comment lines: 1770
# Code lines: 10663
# Change this value based on the comment symbol used in your programming
# language.
commentSymbol = "#"
import sys
import os, os.path
acceptableFileExtensions = sys.argv[1:]
if not acceptableFileExtensions:
print('Please pass at least one file extension as an argument.')
quit()
currentDir = os.getcwd()
filesToCheck = []
for root, _, files in os.walk(currentDir):
for f in files:
fullpath = os.path.join(root, f)
if '.git' not in fullpath:
for extension in acceptableFileExtensions:
if fullpath.endswith(extension):
filesToCheck.append(fullpath)
if not filesToCheck:
print( 'No files found.')
quit()
lineCount = 0
totalBlankLineCount = 0
totalCommentLineCount = 0
print('')
print('Filename\tlines\tblank lines\tcomment lines\tcode lines')
for fileToCheck in filesToCheck:
with open(fileToCheck) as f:
fileLineCount = 0
fileBlankLineCount = 0
fileCommentLineCount = 0
for line in f:
lineCount += 1
fileLineCount += 1
lineWithoutWhitespace = line.strip()
if not lineWithoutWhitespace:
totalBlankLineCount += 1
fileBlankLineCount += 1
elif lineWithoutWhitespace.startswith(commentSymbol):
totalCommentLineCount += 1
fileCommentLineCount += 1
print(os.path.basename(fileToCheck) + \
"\t" + str(fileLineCount) + \
"\t" + str(fileBlankLineCount) + \
"\t" + str(fileCommentLineCount) + \
"\t" + str(fileLineCount - fileBlankLineCount - fileCommentLineCount))
print('_____________________________________________')
print('\nScan for all files with fileextension: ' + str(acceptableFileExtensions[0]) + ' finished!')
print('Your Scanresults')
print('---------------------------------------------')
print('Fileextension: ' + str(acceptableFileExtensions[0]))
print('Lines: ' + str(lineCount))
print('Blank lines: ' + str(totalBlankLineCount))
print('Comment lines: ' + str(totalCommentLineCount))
print('Code lines: ' + str(lineCount - totalBlankLineCount - totalCommentLineCount))
print('---------------------------------------------\n\n')