Python script implements code row count statistics for code sharing, and python row count

Source: Internet
Author: User

Python script implements code row count statistics for code sharing, and python row count

Previously implemented with bash (http://www.bkjia.com/article/61943.htm), but that can not be used in windows, so I wrote a python version, it is also convenient for me to use ...... I will not talk about it here, but google doesn't understand it.

Implementation Code
Copy codeThe 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
Rochelle COUNT = len (open (fname). readlines ())
Print fname, ":", l_count
LinesCount + = l_count
Else:
Print ext, ": not support"
Except t:
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 ")

Usage
1. For this directory
Copy codeThe Code is as follows:
./Count. py

2. Count multiple directories
Copy codeThe Code is as follows:
./Count. py/tmp ~

Running Effect

Okay, that's all. I hope it will help you.

Related Article

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.