Python script used to count the total number of code lines in a project

Source: Internet
Author: User

Python script used to count the total number of code lines in a project

This article mainly introduces the Python script used to count the total number of lines of code in the project. This article provides the implementation code directly. For more information, see

Recently, I need to calculate the total number of lines of code in the project and write a Python applet. I have to say how concise Python is. If I use Java to write it, at least twice the current code.

[Code]

Import OS

Path = "/Users/rony/workspace/ecommerce/hot-deploy /"

Global totalcount

Totalcount = 0

Def cfile (path ):

Allfiles = OS. listdir (path)

For file in allfiles:

Child = OS. path. join (path, file)

If OS. path. isdir (child ):

Cfile (child)

Else:

Filename, fileext = OS. path. splitext (child)

Print (fileext)

# File type need to calculate

If fileext in ['. java ','. jsp ', '.html', '.htm ','. xml ','. SQL ','. js ','. ftl ', '.css ','. groovy ']:

Countf = len (open (child, 'ru '). readlines ())

Global totalcount

Totalcount = totalcount + countf;

Print (child)

Print (countf)

Cfile (path)

Print (totalcount)

The code analysis is here. The example is relatively simple.

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.