Count lines of code

import os, re, sys
from os.path import join

rootdir = '/'
filter = '.*\.js$|.*\.py$|.*\.html$|.*\.css$|.*\.sql$|.*\.cs$|.*\.cpp$|.*\.c$|.*\.h|.*\.java$'

def usage():
    print 'USAGE: python /<rootdir>'

def loc(fname):
        f = open(fname)
        return len(f.readlines())
        return 0

def scan(dir):
    total = 0
    for root, dirs, files in os.walk(dir):
        for file in files:
            if re.match(filter, file):
                fullpath = os.path.join(root, file)
                ln = loc(fullpath)
                print fullpath, ln
                total += ln

    print '\n', '_'*34, '\n\n', 'Total # Of lines:', total, '\n\n'

if __name__ == '__main__':

    fname = ''
    args = sys.argv

    if len(args) >= 2:
        fname = args[1]
        fname = raw_input("Enter root directory: ")


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s