I worked on a project involving xyz lines of code ...

How many times while editing your resume you wanted to add the above statement?

There is a simpler way to achieve that using the shell (e.g. wc -l $(find path/to/my/project -name \*.cc) | sort -nr | head -20), but anyway here is portable solution based on Python.

Usage: <root directory> <print line counts per file? T : F> [<file suffix>*]

e.g. ./countLines.py . T ".cpp" ".h" , prints the total count of lines in all .cpp and .h files under the file tree rooted at the current directory and the number of lines per file included.

To include all files, do not specify a suffix.

#!/usr/bin/python

import os

import sys

if '__main__' == __name__:

if len(sys.argv) < 3:

print 'usage: <root directory> <print file names? T : F> [<file suffix>*]';

print 'e.g. ./countLines.py . T ".cpp" ".h" , in order to count the lines in all .cpp and .h files in the file tree rooted at the current directory and print the number of lines per file included.'

print 'To include all files, do not specify a suffix.'

assert(False);

topDir = sys.argv[1];

bPrint = sys.argv[2] == 'T';

typeFilters = [];

if len(sys.argv) < 4: # what if all files should be included?

typeFilters.append(lambda x : True);

else:

for index in range(3,len(sys.argv)):

suffix = sys.argv[index];

typeFilters.append(lambda x : len(x.split(suffix)) > 1 and x.split(suffix)[-1] == '');

cntFiles = 0;

cntLines = 0;

import operator;

for root, dirs, files in os.walk(topDir):

for name in filter(lambda x : reduce(operator.or_ , map(lambda y : y(x), typeFilters), False), files):

cntFiles = cntFiles + 1;

f = open(os.path.join(root,name), 'r');

old = cntLines;

for line in f:

cntLines = cntLines + 1;

if bPrint:

print cntLines-old, ' lines in', os.path.join(root,name)

f.close();

print cntLines, ' lines in', cntFiles, ' files'

As always criticism, comments, or suggestions are welcome.