使用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实现查找字符串数组最长公共前缀。分享给大家供大家参考,具体如下: 编写一个函数来查找字符串数组中的最长公共前缀。 class Solution: def...

django model去掉unique_together报错的解决方案

事情是这样的,我有一个存储考试的表 class Exam(models.Model): category = cached_fields.ForeignKeyField(Categ...

Python代码的打包与发布详解

在python程序中,一个.py文件被当作一个模块,在各个模块中定义了不同的函数。当我们要使用某一个模块中的某一个函数时,首先须将这个模块导入,否则就会出现函数未定义的情况. 下面记录的...

pandas的相关系数与协方差实例

1、输出百分比变化以及前后指定的行数 a = np.arange(1,13).reshape(6,2) data = DataFrame(a) #计算列的百分比变化,如果...

Python开发微信公众平台的方法详解【基于weixin-knife】

本文实例讲述了Python开发微信公众平台的方法。分享给大家供大家参考,具体如下: 这两天将之前基于微信公众平台的代码重构了下,基础功能以库的方式提供,提供了demo使用的是django...