[python]代码库
import os
#count the line of a single file
def CountLine(path):
tempfile = open(path)
res = 0
for lines in tempfile:
res += 1
print "%s %d" %(path, res) #output the file path and lines
return res
#count the total line of a folder, sub folder included
def TotalLine(path):
total = 0
for root, dirs, files in os.walk(path):
for item in files:
ext = item.split('.')
ext = ext[-1] #get the postfix of the file
if(ext in ["cpp", "c", "h", "java", "py", "php"]):
subpath = root + "/" + item
total += CountLine(subpath)
return total
print "Input Path"
path = raw_input()
print TotalLine(path)
中级程序员
by: 阿小 发表于:2013-08-24 17:26:30 顶(1) | 踩(0) 回复
很好,很强大,学习了!
回复评论