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.