zoukankan      html  css  js  c++  java
  • 代码行数统计(python实现)

    之前用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,os
    
    extens = [".c",".cpp",".hpp",".h"]
    linesCount = 0
    filesCount = 0
    
    def 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!"
                    pass
    
    
    if len(sys.argv) > 1 :
        for m_dir in sys.argv[1:]:        
            print m_dir
            funCount(m_dir)
    else :
        funCount(".")        
        
    print "files count : ",filesCount
    print "lines count : ",linesCount
    
    raw_input("Press Enter to continue"

    使用方法

    1、针对本目录

    ./count.py

    2、统计多个目录

    ./count.py /tmp ~

    运行效果

    好,就这些了,希望对你有帮助。

  • E-Mail : Mike_Zhang@live.com
  • 转载请注明出处,谢谢!
查看全文
  • 相关阅读:
    [LeetCode] 1131. Maximum of Absolute Value Expression 绝对值表达式的最大值
    [LeetCode] 1130. Minimum Cost Tree From Leaf Values 叶值的最小代价生成树
    [LeetCode] 1129. Shortest Path with Alternating Colors 颜色交替的最短路径
    [LeetCode] 1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量
    [LeetCode] 1125. Smallest Sufficient Team 最小的必要团队
    [LeetCode] 1124. Longest Well-Performing Interval 表现良好的最长时间段
    [LeetCode] 1122. Relative Sort Array 数组的相对排序
    Gitalk 自动初始化评论
    [LeetCode] 1111. Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度
    [LeetCode] 1110. Delete Nodes And Return Forest 删点成林
  • 原文地址:https://www.cnblogs.com/MikeZhang/p/linesCountPython20120823.html
  • Copyright © 2011-2022 走看看