Python统计代码行数

import os,shutil

total = 0

def HandleDir(root, dir):
    pass

def DoFilter(file):
    focus = [".lua"]
    ext = os.path.splitext(file)[-1]
    if ext in focus:
        return True
    return False


def HandleFile(root, file):
    global total
    if not DoFilter(file):
        return
    full_path = os.path.join(root, file)
    lines = open(full_path).readlines()
    total = total + len(lines)
    print os.path.join(root, file)
    print len(lines)

def WalkThroughPath(dst_path):
    try:
        for root, dirs, files in os.walk(dst_path):
            for dir in dirs:
                HandleDir(root, dir)
            for file in files:
                HandleFile(root, file)
        print total
    except WindowsError:
        pass

def main():
    WalkThroughPath("D:\Tmp\jishi_win7_lr")

if __name__ == "__main__":
    main()

 

代码如下:

 

posted @ 2013-07-25 18:02  20082663  阅读(234)  评论(0编辑  收藏  举报