使用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实现自动化上线脚本的示例

程序说明: 本程序实现将开发程序服务器中的打包文件通过该脚本上传到正式生产环境(注:生产环境和开发环境不互通) 程序基本思路: 将开发环境中的程序包拷贝到本地堡垒机 将程序包进行解压 获...

Python @property使用方法解析

1. 作用 将类方法转换为类属性,可以用 . 直接获取属性值或者对属性进行赋值 2.实现方式 使用property类来实现,也可以使用property装饰器实现,二者本质是一样的。多...

使用Python3 编写简单信用卡管理程序

1、程序执行代码: #Author by Andy #_*_ coding:utf-8 _*_ import os,sys,time Base_dir=os.path.dirname...

Python中list查询及所需时间计算操作示例

本文实例讲述了Python中list查询及所需时间计算操作。分享给大家供大家参考,具体如下: # -*-coding=utf-8 -*- #! python2 #filename:...

python中xrange和range的区别

range 函数说明:range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列。range示例:复制代码 代码如下...