用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统计文本文件内单词数量的方法.分享给大家供大家参考.具体实现方法如下: # 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统计python文件中代码,注释及空白对应的行数示例【测试可用】
本文实例讲述了Python实现统计python文件中代码,注释及空白对应的行数.分享给大家供大家参考,具体如下: 其实代码和空白行很好统计,难点是注释行 python中的注释分为以#开头的单行注释 或者以'''开头以'''结尾 或以"""开头以"""结尾的文档注释,如: ''' hello world ''' 和 ''' hello world''' 思路是用is_comment记录是否存在多行注释,如果不存在,则判断当前行是否以'''开头,是则
-
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统计文件中去重后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统计文件行数示例分享
复制代码 代码如下: 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实现对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脚本实现统计日志文件中的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
随机推荐
- 深入分析jsonp协议原理
- 木马程序Trojan-Spy.Win32.Agent.cfu清除方法
- 怎么选择Javascript框架(Javascript Framework)
- Python和JavaScript间代码转换的4个工具
- 使用jsonp实现跨域获取数据实例讲解
- PHP文件锁定写入实例解析
- Python实现的爬虫功能代码
- Android开发工程中集成mob短信验证码功能的方法
- MySQL创建带特殊字符的数据库名称方法示例
- JS限定手机版中图片大小随分辨率自动调整的方法
- C#转换日期类型实例
- 数据库Left join , Right Join, Inner Join 的相关内容,非常实用
- Java编程中10个最佳的异常处理技巧
- Jquery Ajax请求代码(2)
- Meta 标签生成器
- javascript数组输出的两种方式
- JavaScript优化专题之Loading and Execution加载和运行
- Android时间选择器、日期选择器实现代码
- Java实现调用jython执行python文件的方法
- 使用Angular CLI进行Build(构建)和Serve详解
