Python script implements code line count code sharing _python

Source: Internet
Author: User

It has been implemented in bash before (http://www.jb51.net/article/61943.htm), but that can't be used under Windows, so it's a Python version, and it's convenient for me to use later ... Here is not much introduced, do not understand the Google under.

Implementation code

Copy Code code as follows:

#!/usr/bin/python

'''
File:count.py
Author:mike
E-mail:mike_zhang@live.com
'''
Import Sys,os

Extens = [". C", ". cpp", ". hpp", ". H"]
Linescount = 0
Filescount = 0

def funcount (dirname):
Global Extens,linescount,filescount
For Root,dirs,filenames in Os.walk (dirname):
For F in FileNames:
fname = Os.path.join (root,f)
Try:
ext = F[f.rindex ('. '):]
if (extens.count (EXT) > 0):
print ' Support '
Filescount + 1
Print fname
L_count = Len (open (fname). ReadLines ())
Print fname, ":", L_count
Linescount + + L_count
Else
Print ext, ": not support"
Except
Print "Error occur!"
Pass


If Len (SYS.ARGV) > 1:
For M_dir in sys.argv[1:]:
Print M_dir
Funcount (M_dir)
else:
Funcount (".")

print "Files Count:", Filescount
Print "Lines count:", Linescount

Raw_input ("Press Enter to continue")

How to use
1, for this directory

Copy Code code as follows:

./count.py

2. Count Multiple Catalogs
Copy Code code as follows:

./count.py/tmp ~

Run effect

Well, that's all, I hope to help you.

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.