使用python统计文件行数示例分享

yipeiwu_com6年前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 sum(line.count("\n") for line in block(f))
if __name__ == "__main__":
    import sys
    import os
    if len(sys.argv) != 2:
        print("error imput argument")
        sys.exit(-1)
    if not os.path.isfile(sys.argv[1]) :
       print(sys.argv + " is not a file")
       sys.exit(-1)
    start_time = time.time()
    print(getLineCount(sys.argv[1]))
    print(time.time() - start_time ,"seconds")

相关文章

Python的numpy库中将矩阵转换为列表等函数的方法

这篇文章主要介绍Python的numpy库中的一些函数,做备份,以便查找。 (1)将矩阵转换为列表的函数:numpy.matrix.tolist() 返回list列表 Examples...

python生成二维码的实例详解

python生成二维码的实例详解 版本相关 操作系统:Mac OS X EI Caption Python版本:2.7 IDE:Sublime Text 3 依赖库 Pyth...

python使用arp欺骗伪造网关的方法

本文实例讲述了python使用arp欺骗伪造网关的方法。分享给大家供大家参考。具体实现方法如下: #coding:utf-8 ''' arp欺骗局域网pc,将伪造的网关mac以网关的...

Python 3.x基于Xml数据的Http请求方法

Python 3.x基于Xml数据的Http请求方法

1. 前言 由于公司的一个项目是基于B/S架构与WEB服务通信,使用XML数据作为通信数据,在添加新功能时,WEB端与客户端分别由不同的部门负责,所以在WEB端功能实现过程中,需要自己发...

Python注释、分支结构、循环结构、伪“选择结构”用法实例分析

Python注释、分支结构、循环结构、伪“选择结构”用法实例分析

本文实例讲述了Python注释、分支结构、循环结构、伪“选择结构”用法。分享给大家供大家参考,具体如下: 注释: python使用#作为行注释符,使用三引号作为多行注释符 分支结构:...