如何使用Python计算java源代码的注释行和空行?

2024-04-24 19:43:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的学习者。这个是我写的程序。但似乎有很多问题。我想计算总线条、空白线条、注释线条和代码行。但我不能运行这个程序。我知道我计算评论行的方法是错了。真的吗需要帮助。在

def FileLineCount(filename):
    (shotname,extension) = os.path.splitext(filename);
    if extension == '.java' : # file type 
        file = open(filename,'r');
        allLines = file.readlines();
        file.close();

        lineCount    =0;
        commentCount = 0;
        blankCount   = 0;
        codeCount    = 0;
        for eachLine in allLines:
                if  eachLine.find('//'):  #LINECOMMENT 
                    commentCount += 1;
                else :
                    if eachLine == "":
                        blankCount += 1;
                    else :
                        codeCount += 1;
            lineCount = lineCount + 1;
        self.all += lineCount;
        self.allComment += commentCount;
        self.allBlank += blankCount;
        self.allSource += codeCount;
        print filename;
        print '           Total      :',lineCount ;
        print '           Comment    :',commentCount;
        print '           Blank      :',blankCount;
        print '           Source     :',codeCount;

Tags: self程序ifextensionfilenameelse线条file