用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统计纯文本文件中英文单词出现个数的方法.分享给大家供大家参考,具体如下: 第一版: 效率低 # -*- 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实现统计文本文件字数的方法
本文实例讲述了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统计python文件中代码,注释及空白对应的行数示例【测试可用】
本文实例讲述了Python实现统计python文件中代码,注释及空白对应的行数.分享给大家供大家参考,具体如下: 其实代码和空白行很好统计,难点是注释行 python中的注释分为以#开头的单行注释 或者以'''开头以'''结尾 或以"""开头以"""结尾的文档注释,如: ''' hello world ''' 和 ''' hello world''' 思路是用is_comment记录是否存在多行注释,如果不存在,则判断当前行是否以'''开头,是则
-
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实现对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*'
-
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统计文件行数示例分享
复制代码 代码如下: 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统计文本文件内单词数量的方法.分享给大家供大家参考.具体实现方法如下: # 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/
随机推荐
- AngularJS实现动态添加Option的方法
- perl特殊符号及默认的内部变量
- vue实现app页面切换动画效果实例
- c#入门之分支语句使用方法(三元运算符、if语句、switch语句)
- JS实现随机颜色的3种方法与颜色格式的转化
- delphi发送消息控制滚动条使用示例
- mysql5.7.17.msi安装图文教程
- Android RecyclerView点击事件
- 本地Bootstrap文件字体图标引入却无法显示问题的解决方法
- JSP迅速入门
- servlet重定向详解(八)
- 跨浏览器的设置innerHTML方法
- JQuery-tableDnD 拖拽的基本使用介绍
- JavaScript中的setUTCDate()方法使用详解
- 解密DDoS攻击——“缓存溢出”新变形
- C#使用foreach语句遍历集合类型的方法
- 很实用的Android日期计算类
- 利用SpringMVC过滤器解决vue跨域请求的问题
- 解决Layui选择全部,换页checkbox复选框重新勾选的问题方法
- Python使用LDAP做用户认证的方法