Skip to content

Instantly share code, notes, and snippets.

@RyanFleck
Created July 10, 2019 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanFleck/21cbda198d1b8af9e078ebc48735dec7 to your computer and use it in GitHub Desktop.
Save RyanFleck/21cbda198d1b8af9e078ebc48735dec7 to your computer and use it in GitHub Desktop.
Count lines of code for a given extension type in a repository.
#!/usr/bin/python3
# Takes file extension as first argument, counts lines of code.
import os
import sys
import re
def countLinesOfCode(extension):
print(r'(.*\.{}$)'.format(extension))
loc = 0
fileregex = re.compile(r'(.*\.{}$)'.format(extension))
for dirname, subdirs, filenames in os.walk(os.getcwd()):
for filename in filenames:
if fileregex.match(filename):
lines = sum(1 for line in open(dirname+'/'+filename))
print('> '+str(filename)+' => '+str(lines)+' lines.')
loc = loc + lines
print('\n>> '+str(loc)+' lines of '+str(extension))
try:
countLinesOfCode(str(str(sys.argv[1]).strip().split()[0]))
except IndexError:
print('Please provide an extension to count LoC for.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment