使用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的mysql数据库建立表与插入数据操作示例

本文实例讲述了python的mysql数据库建立表与插入数据操作。分享给大家供大家参考,具体如下: mysql数据库建立表 一 代码 import pymysql # 打开数据库连接...

在python中创建指定大小的多维数组方式

python中创建指定大小的二维数组,有点像C++中进行动态申请内存创建数组,不过相比较而言,python中更为简单一些。 创建n行m列的二维数组: n = 2 m = 3 ma...

pycharm配置当鼠标悬停时快速提示方法参数

pycharm配置当鼠标悬停时快速提示方法参数

1、配置pycharm 依次点击"File"-->"Settings",进入"Editor"-->"General",勾选"Other"下的“Show quick docum...

利用Python和OpenCV库将URL转换为OpenCV格式的方法

利用Python和OpenCV库将URL转换为OpenCV格式的方法

今天的博客是直接来源于我自己的个人工具函数库。 过去几个月,有些PyImageSearch读者电邮问我:“如何获取URL指向的图片并将其转换成OpenCV格式(不用将其写入磁盘再读...

python reduce 函数使用详解

python reduce 函数使用详解

reduce() 函数在 python 2 是内置函数, 从python 3 开始移到了 functools 模块。 官方文档是这样介绍的 reduce(...) reduce(fu...