程式碼數統計(python實現)

來源:互聯網
上載者:User

之前用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 ~

運行效果

好,就這些了,希望對你有協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.