Python script implementation code line count code sharing

Source: Internet
Author: User
Before using Bash (http://www.bitsCN.com/article/61943.htm), but that can't be used under Windows, so I wrote a python version, it is also convenient for me to use later ... Here is not more introduced, do not understand the Google under.

Implementation code

The code is 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 catalogue

The code is as follows:


./count.py


2. Count Multiple Catalogs

The code is as follows:


./count.py/tmp ~


Run effect

All right, that's it, I hope it helps 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.