用python统计代码行的示例(包括空行和注释)
实例如下所示:
import os
import string
path = "/Users/U/workspace/python learning/show-me-the-code/0007/test/"
dir = os.listdir(path)
def count(file):
total = 0 #总行数
countPound = 0 #注释行数
countBlank = 0 #空行数
line = open(file,'r',encoding='utf-8') #打开文件,因为注释有中文所以使用utf-8编码打开
for li in line.readlines(): #readlines()一次性读完整个文件
total += 1
if not li.split(): #判断是否为空行
countBlank +=1
li.strip()
if li.startswith('#'):
countPound += 1
print(file)
print("countBlank:%d" % countBlank)
print("countPound:%d" % countPound)
print("total:%d" % total)
for file in dir:
count(path + file)
以上这篇用python统计代码行的示例(包括空行和注释)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
相关推荐
-
Python实现统计文本文件字数的方法
本文实例讲述了Python实现统计文本文件字数的方法.分享给大家供大家参考,具体如下: 统计文本文件的字数,从当前目录下的file.txt取文件 # -*- coding: GBK -*- import string import sys reload(sys) def compareItems((w1,c1), (w2,c2)): if c1 > c2: return - 1 elif c1 == c2: return cmp(w1, w2) else: return 1 def main()
-
使用python统计文件行数示例分享
复制代码 代码如下: import time def block(file,size=65536): while True: nb = file.read(size) if not nb: break yield nb def getLineCount(filename): with open(filename,"r",encoding="utf-8") as f: return
-
Python统计文件中去重后uuid个数的方法
本文实例讲述了Python统计文件中去重后uuid个数的方法.分享给大家供大家参考.具体如下: 利用正则表达式按行获取日志文件中的的uuid,并且统计这些uuid的去重个数(去重利用set) import re pattern=re.compile(r'&uuid=.*&') uuidset=set() with open('request.log.2015-05-26','rt') as f: for line in f: all=pattern.findall(line) if len
-
python 远程统计文件代码分享
python 远程统计文件 #!/usr/bin/python #encoding=utf-8 import time import os import paramiko import multiprocessing #统计文件数量 def get_total(ip,password,filepath): paramiko.util.log_to_file('paramiko.log') ssh=paramiko.SSHClient() ssh.set_missing_host_key_poli
-
Python统计python文件中代码,注释及空白对应的行数示例【测试可用】
本文实例讲述了Python实现统计python文件中代码,注释及空白对应的行数.分享给大家供大家参考,具体如下: 其实代码和空白行很好统计,难点是注释行 python中的注释分为以#开头的单行注释 或者以'''开头以'''结尾 或以"""开头以"""结尾的文档注释,如: ''' hello world ''' 和 ''' hello world''' 思路是用is_comment记录是否存在多行注释,如果不存在,则判断当前行是否以'''开头,是则
-
python统计文本文件内单词数量的方法
本文实例讲述了python统计文本文件内单词数量的方法.分享给大家供大家参考.具体实现方法如下: # count lines, sentences, and words of a text file # set all the counters to zero lines, blanklines, sentences, words = 0, 0, 0, 0 print '-' * 50 try: # use a text file you have, or google for this one
-
python脚本实现统计日志文件中的ip访问次数代码分享
适用的日志格式: 106.45.185.214 - - [06/Aug/2014:07:38:59 +0800] "GET / HTTP/1.0" 200 10 "-" "-" 171.104.119.22 - - [06/Aug/2014:08:55:01 +0800] "GET / HTTP/1.0" 200 10 "-" "-" 27.31.238.242 - - [06/Aug/
-
Python3读取UTF-8文件及统计文件行数的方法
本文实例讲述了Python3读取UTF-8文件及统计文件行数的方法.分享给大家供大家参考.具体实现方法如下: ''''' Created on Dec 21, 2012 Python 读取UTF-8文件 统计文件的行数目 @author: liury_lab ''' # -*- coding: utf-8 -*- import codecs # 对较小的文件,最简单的方法是将文件读入一个行列表中, # 然后计算列表的长度即可 count = len(codecs.open('d:/FreakOu
-
Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】
本文实例讲述了Python统计纯文本文件中英文单词出现个数的方法.分享给大家供大家参考,具体如下: 第一版: 效率低 # -*- coding:utf-8 -*- #!python3 path = 'test.txt' with open(path,encoding='utf-8',newline='') as f: word = [] words_dict= {} for letter in f.read(): if letter.isalnum(): word.append(letter)
-
Python实现对excel文件列表值进行统计的方法
本文实例讲述了Python实现对excel文件列表值进行统计的方法.分享给大家供大家参考.具体如下: #!/usr/bin/env python #coding=gbk #此PY用来统计一个execl文件中的特定一列的值的分类 import win32com.client filename=raw_input("请输入要统计文件的详细地址:") flag=0 #用于判断文件 名如果不带'日'就为 0 if '\xc8\xd5' in filename:flag=1 print 50*'
随机推荐
- 用VBSrcipt判断是否是日期
- 详解微信小程序——自定义圆形进度条
- Struts2 $,#,%详解及实例代码
- 浅述python中argsort()函数的实例用法
- 真见识了-全代码编写的图片
- C语言位运算和sizeof运算符详解
- php获取当前月与上个月月初及月末时间戳的方法
- 用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试
- JavaScript语言对Unicode字符集的支持详解
- 只防病毒不安全 网络安全还要防哪些?
- 不用杀毒软件也能让系统百毒不侵的技巧
- 利用stream实现一个简单的http下载器
- Android 隐藏及切换显示键盘
- Java中的InputStreamReader和OutputStreamWriter源码分析_动力节点Java学院整理
- C#文件后缀名的详细介绍
- 基于SQL Server中如何比较两个表的各组数据 图解说明
- JS函数节流和函数防抖问题分析
- C语言对对碰游戏源码分享
- Nodejs中怎么实现函数的串行执行
- Select2在使用ajax获取远程数据时显示默认数据的方法
