Python 练习 06 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释
2018-01-21 15:30
846 查看
#有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释 import os import re os.chdir('文件路径') fh=open('文件名', 'r', encoding='utf-8') read_fh=fh.readlines() fh.close() number_code=0 number_empty=0 number_note=0 pattern='.*#' #正则匹配模式 for x in read_fh: if '#' in x : #计算注释数目 if re.findall(pattern,x)[0][:-1].isspace() or re.findall(pattern,x)[0][:-1]=='': number_note+=1 else: number_code+=1 elif x.isspace(): number_empty+=1 else: number_code+=1 print ('code number is %d'%(number_code+number_empty+number_note)) print ('empty number is %d'%number_empty) print ('note number is %d'%nu 4000 mber_note)
2.
import os.pat def mainKeywords(dirPath): blank, comments, codelines, totalines, count, temp = 0, 0, 0, 0, 0, 0 f_list = os.listdir(dirPath) for i in f_list: if os.path.splitext(i)[1] == '.py': print(i) with open(i, 'r', encoding='utf-8') as fp: while True: line = fp.readline() totalines += 1 if not line: break elif line.strip().startswith('#'): comments += 1 elif line.strip().startswith("'''") or line.strip().startswith('"""'): comments += 1 if line.count('"""') == 1 or line.count("'''") == 1: while True: line = fp.readline() totalines += 1 comments += 1 if ("'''" in line) or ('"""' in line): break elif line.strip(): codelines += 1 else: blank += 1 print('the nuber of totalines is : ' + str(totalines-1)) print('the nuber of comments is : ' + str(comments)) print('the nuber of codelines is : ' + str(codelines)) print('the nuber of blanklines is : ' + str(blank)) blank, comments, codelines, totalines = 0, 0, 0, 0 mainKeywords('E:/Users/summer/PycharmProjects/untitled')
相关文章推荐
- 每天一个python小程序 007有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
- 第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
- 第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
- 用python统计代码行(包括空行和注释)
- python 统计一个目录下代码的行数、空行和注释
- python写的统计PYTHON文件的(代码行数,空行数,注释行数)小程序
- 用python统计代码行的示例(包括空行和注释)
- Python Show-Me-the-Code 第 0007 题 统计代码行数(注释,空行,总行数)小程序
- python统计代码空行与注释
- Python实现代码统计(代码+注释+空行)
- PHP统计目录下的文件总数及代码行数(去除注释及空行)
- python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数
- PHP统计目录下的文件总数及代码行数(去除注释及空行)
- PHP统计目录下的文件总数及代码行数(去除注释及空行)
- 第0007道练习题_Python统计代码行数注释行数空白行数
- Python3 统计代码 - 练手小程序(一)
- 统计一下你写过多少代码
- python 统计代码行数,注释行数的简单实现
- 批处理-统计一下当前目录中到底有多少种类型的文件