之前用bash實現過(http://www.cnblogs.com/MikeZhang/archive/2012/08/22/linesCountBash20120822.html ),不過那個不能在windows下使用,所以就寫了個python版,也方便我以後使用……這裡就不多介紹了,不懂的google下。
實現代碼
#!/usr/bin/python''' File : count.py Author : Mike E-Mail : Mike_Zhang@live.com'''import sys,osextens = [".c",".cpp",".hpp",".h"]linesCount = 0filesCount = 0def funCount(dirName): global extens,linesCount,filesCount for root,dirs,fileNames in os.walk(dirName): for f in fileNames: fname = os.path.join(root,f) try : ext = f[f.rindex('.'):] if(extens.count(ext) > 0): print 'support' filesCount += 1 print fname l_count = len(open(fname).readlines()) print fname," : ",l_count linesCount += l_count else: print ext," : not support" except: print "Error occur!" passif len(sys.argv) > 1 : for m_dir in sys.argv[1:]: print m_dir funCount(m_dir)else : funCount(".") print "files count : ",filesCountprint "lines count : ",linesCountraw_input("Press Enter to continue")
使用方法
1、針對本目錄
./count.py
2、統計多個目錄
./count.py /tmp ~
運行效果
好,就這些了,希望對你有協助。