Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Python统计文件行数 | LinuxHub #130

Open
jwenjian opened this issue Oct 25, 2019 · 0 comments
Open

Python统计文件行数 | LinuxHub #130

jwenjian opened this issue Oct 25, 2019 · 0 comments

Comments

@jwenjian
Copy link
Owner

jwenjian commented Oct 25, 2019

统计一个2.6G的日志文件的行数. 哪种方法会快一些呢?

这里列出了4种方法:


结论是第四种,即 使用enumerate循环读取的方法,拿索引

import time
start_time = time.time()
 
count = -1
for count,line in enumerate(open("/tmp/20151210_api.linuxhub.org.log",'r')):
    count += 1
print count
 
print time.time() - start_time,"seconds"

原文 -> https://www.linuxhub.org/?p=3104

@jwenjian jwenjian changed the title [From Instapaper] : Python统计文件行数 | LinuxHub Python统计文件行数 | LinuxHub Oct 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant