手机
当前位置:查字典教程网 >脚本专栏 >python >使用python统计文件行数示例分享
使用python统计文件行数示例分享
摘要:复制代码代码如下:importtimedefblock(file,size=65536):whileTrue:nb=file.read(si...

复制代码 代码如下:

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连接池实现示例程序

使用python提取html文件中的特定数据的实现代码

学习python的几条建议分享

python网络爬虫采集联想词示例

python实现人人网登录示例分享

使用python的chardet库获得文件编码并修改编码

python合并文本文件示例

python解析文件示例

用python实现批量重命名文件的代码

Python读写Excel文件的实例

精品推荐
分类导航