To make it easier to count the lines of your own code, you implemented the gadget in Python, which enables you to count multiple files in a single file and a directory, and output the number of lines of code, blank lines, and comment lines. For code in different languages, it is only necessary to modify its annotation to correctly count the number of lines of information.
Import OS, RE, sys
Class Codecount:
"""
This are used to calculate total number of source,
Comment and Blank lines.
"""
def __init__ (self, path, commentsign = ' # '):
Self.path = Path
Self.all = 0
self.allcomment = 0
Self.allblank = 0
Self.allsource = 0
Self.commentsign = Commentsign
def Calculate (self):
If not Os.path.isdir (Self.path):
If Os.path.exists (Self.path):
Self. Countlines (Self.path)
else:
Print '%s is not a valid path or file name! '% path
Return
For Root, dirs, the files in Os.walk (Self.path):
For file in Files:
File = Os.path.join (root, file)
Self. Countlines (file)
def countlines (self, filename):
f = file (filename)
lines = F.readlines ()
Rblank = Re.compile (R ' ^s*$ ')
Rcomment = Re.compile (' ^s*%sw* '% self.commentsign)
Nblank = 0
ncomment = 0
Nsource = 0
For line in lines:
If Rblank.match (line) or not:
Nblank + 1
Elif Rcomment.match (line):
Ncomment + 1
else:
Nsource + 1
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.