Recently, I helped my friends build a foreign trade management system project. I spent two months on it and wrote a lot of java code and xhtml files. I was satisfied with my sense of accomplishment, I plan to write a program to calculate the total number of lines of code. I recently learned the Python program. So I chose Python as the implementation language. As I am a newbie, therefore, if the writing is insufficient, please instruct the program as follows (count the number of java files in a specific directory): [python] ''' count file lines number by directory @ author: aihua. sun '''import OS. path; #-*-coding: UTF-8-*-import OS; import string; import codecs; resultFile = open ("result. file "," w + "); DOC_TAG =" ''' "; FILE_COUNT = 0; FILE _ EXTENSION = ". java "; LINE_NUMBER = 0; COUNT_LINE_NUMBER = True; DEFAULT_DIR =" D: \ sourcecode \ FTS "; def collection (): dirpath = input (" please input module directionary :"); if OS. path. isdir (dirpath): partition tree (dirpath); else: print ("please input a direary ary path! "); # Use default directory dirpath = DEFAULT_DIR; tree (dirpath); print (" DIRECTOTY: % s FILE_EXTENSION: % s LINE_NUMBER: % s "% (dirpath, FILE_EXTENSION, LINE_NUMBER) def spanning tree (dirpath): OS. chdir (dirpath); fileExtensionLen = len (FILE_EXTENSION); for subFile in OS. listdir (dirpath): # print (OS. getcwd (); if OS. path. isdir (subFile): fullSubFile = str (OS. getcwd () + str (OS. sep) + subFile; # print (fullSub File); tree (fullSubFile); elif OS. path. basename (subFile) [-fileExtensionLen:] = FILE_EXTENSION: collectDocument (subFile); OS. chdir (OS. path. pardir); def collectDocument (subFile): countDirectoryLineNumber (subFile) resultFile. writelines ("\ n" + "FILE: % s" % (str (OS. getcwd (), str (OS. sep), str (OS. path. basename (subFile); resultFile. writelines ("\ n" + "DOCUMENT:"); appendDocToResultFile (subFile, ResultFile); def countDirectoryLineNumber (subFile): # print (subFile) global LINE_NUMBER; # use "w +" will write clear target file, so it's dangerous open = codecs. open; newFileObject = open (subFile, "r +", 'utf-8'); singleFileLineNumber = len (newFileObject. readlines (); # print (singleFileLineNumber); LINE_NUMBER + = singleFileLineNumber; def appendDocToResultFile (subFile, resultFile): inDocArea = Fals E; for fileLine in subFile: # begin with document if fileLine. find (DOC_TAG)> 0: inDocArea = True; if inDocArea: resultFile. writelines (fileLine); # end of document if inDocArea and fileLine. find (DOC_TAG)> 0: resultFile. writelines (fileLine); resultFile. close (); break; if _ name _ = '_ main _': collection (); ''' count file lines number by directory @ author: aihua. sun '''import OS. path; #-*-coding: ut F-8-*-import OS; import string; import codecs; resultFile = open ("result. file "," w + "); DOC_TAG =" ''' "; FILE_COUNT = 0; FILE_EXTENSION = ". java "; LINE_NUMBER = 0; COUNT_LINE_NUMBER = True; DEFAULT_DIR =" D: \ sourcecode \ FTS "; def collection (): dirpath = input (" please input module directionary :"); if OS. path. isdir (dirpath): partition tree (dirpath); else: print ("please input a direary ary path! "); # Use default directory dirpath = DEFAULT_DIR; tree (dirpath); print (" DIRECTOTY: % s FILE_EXTENSION: % s LINE_NUMBER: % s "% (dirpath, FILE_EXTENSION, LINE_NUMBER) def spanning tree (dirpath): OS. chdir (dirpath); fileExtensionLen = len (FILE_EXTENSION); for subFile in OS. listdir (dirpath): # print (OS. getcwd (); if OS. path. isdir (subFile): fullSubFile = str (OS. getcwd () + str (OS. sep) + subFile; # print (fullSubFile); Decision Tree (fullSubFile); elif OS. path. basename (subFile) [-fileExtensionLen:] = FILE_EXTENSION: collectDocument (subFile); OS. chdir (OS. path. pardir); def collectDocument (subFile): countDirectoryLineNumber (subFile) resultFile. writelines ("\ n" + "FILE: % s" % (str (OS. getcwd (), str (OS. sep), str (OS. path. basename (subFile); resultFile. writelines ("\ n" + "DOCUMENT:"); appendDocToResultFile (subFile, resultFile); def countDirectoryLineNumber (subFile): # print (subFile) global LINE_NUMBER; # use "w +" will write clear target file, so it's dangerous open = codecs. open; newFileObject = open (subFile, "r +", 'utf-8'); singleFileLineNumber = len (newFileObject. readlines (); # print (singleFileLineNumber); LINE_NUMBER + = singleFileLineNumber; def appendDocToResultFile (subFile, resultFile): inDocArea = False; www.2cto.com for fileLine in subFile: # begin with document if fileLine. find (DOC_TAG)> 0: inDocArea = True; if inDocArea: resultFile. writelines (fileLine); # end of document if inDocArea and fileLine. find (DOC_TAG)> 0: resultFile. writelines (fileLine); resultFile. close (); break; if _ name _ = '_ main _': collection ();