Python: A small program that counts the number of lines of code in a specific file in a specific folder

Source: Internet
Author: User

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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.