Python 练习小工具,统计代码行数。Java, C, Object-C, Swift
# -*- coding: utf-8 -*-
#Python 练习小工具,统计Java 代码行数
import os
#统计文件行数
def countLines(fileName):
count = 0
with open(fileName, 'r') as f:
while True:
line = f.readline()
if line:
if len(line)>1:
count += 1
else:
break
#print ">>>%s \tLines %d"%(os.path.split(fileName)[1], count)
return count
#遍历
def traverse(fileName):
if os.path.exists(fileName) == 'false':
print "File is not exist"
return 0
if os.path.isfile(fileName):
type = os.path.splitext(fileName)[1]
if type == '.java' or type == ".c" or type == ".m" or type == ".h" or type == ".swift":
return countLines(fileName)
return 0
if os.path.isdir(fileName):
count = 0
for ddd in os.listdir(fileName):
count += traverse(os.path.join(fileName, ddd))
return count
print "ERROR BUG %s"%fileName
return 0
#Main
abspath = os.path.abspath('.')
count = traverse(abspath)
#count = countLines("hello.txt")
print "\n"
print "*********************************************"
print ">> abspath = %s"%(abspath)
print ">> The number of rows is %d"%(count)
print "*********************************************"
print " Game Over "
print "*********************************************"
网友评论