python简单习题系列6
2014-06-07 16:01
246 查看
写一个脚本统计代码的行数。
由于手工统计代码行数费时费力,不如来一个简单的python 脚本来的快速便捷。顺便练习一下刚刚学习的python语言。
写的只是简单的功能,还是有很多地方需要改进和加强对错误的处理之类。
由于手工统计代码行数费时费力,不如来一个简单的python 脚本来的快速便捷。顺便练习一下刚刚学习的python语言。
写的只是简单的功能,还是有很多地方需要改进和加强对错误的处理之类。
#codeing=UTF-8 # 根据输入的目录,统计该目录下代码行数 import os from os import listdir from os.path import isdir, isfile def CountAFileLines(fileName): file = open(fileName, 'r') allLines = file.readlines(); i = 0 for li in allLines: if li != '': i = i+1 file.close() return i def CodeCount(in_path): # First, check the input path if is valid directory try: assert(isdir(in_path) == True) except AssertionError: in_path = None print ('The input path is not a valid directory!\n') print ('Please input another right path:') # list all files or dirs in input directory # if is a dir, call back # else if is a cpp/h/cs file, count the liens line_number = 0 if(in_path): for one in listdir(in_path): one = "/".join([in_path, one]) curNum = 0 if (isdir(one)): curNum = CodeCount(one) elif (isfile(one)): (filename, extention) = os.path.splitext(one) if(extention == '.h' or extention == '.cpp' or extention == '.cs'): #print all calculate files print (one) curNum = CountAFileLines(one) line_number = line_number + curNum return line_number inputPathStr = input('Please input the code path you want count: \n') print ('\n') print ('List all calculate files: ') line_number = CodeCount(inputPathStr) print ('\nAll codes line number is: ', line_number)
相关文章推荐
- python 简单习题系列1
- python简单习题系列2
- python 简单习题系列5
- python简单习题系列4
- python简单习题系列3
- 简单递推系列 uva习题
- 【数据结构&&算法系列】插入排序简单介绍及python代码
- Appium+Genymotion+robotframework+python系列三:一个简单的Android自动化用例
- python变量和简单数据类型的简单习题
- python各种简单习题汇总
- CDays-2 习题二 (编写类Class,并实现简单的栈)及相关内容解析。Python 基础教程 Class
- python爬虫系列(1)——一个简单的爬虫实例
- Python爬虫系列之----Scrapy(四)一个简单的示例
- 【数据结构&&算法系列】归并排序简单介绍及python代码
- $python爬虫系列(1)——一个简单的爬虫实例
- Python基础系列----环境的搭建及简单输入、输出
- 【python系列】matplotlib简单画图subplot
- python第三方库系列之十六--建立最简单的web服务器
- python 笔记 if语句的简单使用 《笨办法学Python》习题29 ——1.2
- 【MQTT应用学习系列(一)】Apollo代理+paho_mqtt开发Python客户端实现MQTT简单通信