Skip to content

Instantly share code, notes, and snippets.

@TaylorMutch
Created July 13, 2017 21:48
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 TaylorMutch/feca2ddc0009d929639d9d8d6298ce82 to your computer and use it in GitHub Desktop.
Save TaylorMutch/feca2ddc0009d929639d9d8d6298ce82 to your computer and use it in GitHub Desktop.
Print out file statistics from a directory
import os, sys
exts = ['.py', '.ini', '.c', '.h']
count_empty_line = True
here = os.path.abspath(os.path.dirname(sys.argv[0]))
def read_line_count(fname):
count = 0
for line in open(fname).readlines():
if count_empty_line or len(line.strip()) > 0:
count += 1
return count
if __name__ == '__main__':
line_count = 0
file_count = 0
for base, dirs, files in os.walk(here):
for file in files:
# Check the sub directorys
if file.find('.') < 0:
continue
ext = (file[file.rindex('.'):]).lower()
try:
if exts.index(ext) >= 0:
file_count += 1
path = (base + '/'+ file)
c = read_line_count(path)
print(".%s : %d" % (path[len(here):], c))
line_count += c
except:
pass
print('File count : %d' % file_count)
print('Line count : %d' % line_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment