#!/usr/bin/python -tt import sys def count_loc(lines): nb_lines = 0 docstring = False for line in lines: line = line.strip() if line == "" \ or line.startswith("#") \ or docstring and not (line.startswith('"""') or line.startswith("'''"))\ or (line.startswith("'''") and line.endswith("'''") and len(line) >3) \ or (line.startswith('"""') and line.endswith('"""') and len(line) >3) : continue # this is either a starting or ending docstring elif line.startswith('"""') or line.startswith("'''"): docstring = not docstring continue else: nb_lines += 1 return nb_lines if __name__ == '__main__': for file in sys.argv[1:]: f = open(file) print file, count_loc(f) f.close()