Python calculates the total number of. h and. cpp files in a folder,

Source: Internet
Author: User

Python calculates the total number of. h and. cpp files in a folder,

I usually write a lot of code myself, but I never calculate the total number of lines I have written. It's silly to be asked during the interview... Nothing to worry about. Write a python program for statistics.

Import OS ###################################### ######################################## # def calcLine (baseDir): lineCount = 0 try: for fileName in OS. listdir (baseDir): fullPath = baseDir + fileName if OS. path. isdir (fullPath): lineCount + = calcLine (fullPath + '\') # recursively read all files if OS. path. splitext (fullPath) [1] in (". h ",". cpp "): file = open (fullPath) for eachLine in file. readline (): lineCount + = 1 file. close () failed t Exception as e: print (e) return lineCount ###################################### ######################################## # if _ name _ = "_ main __": baseDir = "K: \ C ++ \ MFC \ BubbleDragon \" lineCount = calcLine (baseDir) print (lineCount)

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.