用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脚本实现统计日志文件中的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/
-
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统计文件中去重后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
-
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统计文件行数示例分享
复制代码 代码如下: 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*'
随机推荐
- JS弹出窗口代码大全(详细整理)
- 劲舞团出现问题的解决方法总结
- 剖析ASP.NET MVC的DependencyResolver组件
- python控制台中实现进度条功能
- MySql登陆密码忘记及忘记密码的解决方案
- Json_decode 解析json字符串为NULL的解决方法(必看)
- js实现拉伸拖动iframe的具体代码
- 全面解析Bootstrap排版使用方法(文字样式)
- struts2 action跳转调用另一个程序
- 关于Linux操作系统下终端乱码的完美解决方法
- 一辈子走好一条路(坚持一个行业)
- 简单的Ruby中的Socket编程教程
- 日常收集常用SQL查询语句大全
- jQuery模拟物体自由落体运动(附演示与demo源码下载)
- windows 2008 R2 64位服务器中开启php curl扩展的方法
- PHP curl 获取响应的状态码的方法
- php短址转换实现方法
- C#实现两个时间相减的方法
- 基于文本的留言簿
- 详解Android的MVVM框架 - 数据绑定
