原创Python数代码行数代码,原创python代码行数,如下python代码实现


如下python代码实现数代码行数功能,使用了os模块的walk方法来遍历文件目录,使用enumerate方法来遍历一个代码文件中的所有代码行。

# -*- coding: cp936 -*-import os__author__ = 'byrx.net'def count(path):    for r,dirs,files in os.walk(path):        for f in files:            fileFullPath = os.path.join(r,f)                    left,ex = os.path.splitext(fileFullPath)            ex = ex.lower()            if ex in fileTypes.keys():                fileLineCount = 0                with open(fileFullPath) as fileHandler:                    for index,line in enumerate(fileHandler):                        if len(line.strip()) == 0: # or line.strip().startswith('#'):                            continue                        fileLineCount += 1                print '%s line count is %d' % (fileFullPath,fileLineCount)                fileTypes[ex] += fileLineCount        for d in dirs:            childDir = os.path.join(r,d)            count(childDir)root = r'e:\outofmemory\svn\web'#要数行数的文件类型fileTypes = {'.py':0}count(root)for k,v in fileTypes.items():    print '%s line count is %d' % (k,v)

如果你要使用此代码需要修改root变量的值和要数代码文件的扩展名。

评论关闭