Python 文件行数统计

1 投票
3 回答
6557 浏览
提问于 2025-04-16 15:40

对于下面的内容:

def linecount(filename):
    count = 0
    for x in open(filename):
        count += 1
    return count

这个脚本是怎么“知道”每一行都是一个单独的元素的呢?对于“文件”类型,是否就是通过行来分开的呢?谢谢

3 个回答

0

我对用Python的方法速度感到有些失望。为了得到最快的结果,我选择通过os.popen来调用wc.exe。

int(os.popen("wc -l " + filename).read().strip().split()[0])
2

是的。文件类会从文件中读取数据,每当遇到换行符时,就会生成一个新行。(你可以在iobase.c文件中找到具体的实现,大约在551行附近)

4

因为当你遍历一个 file 对象时,它的表现就像是在遍历:

open(filename).readlines()

但这样做不会占用内存(这对于处理大文件来说是个好处)。


Python 的文档对此有更详细的解释,不过这里有一些重点:

>>> f = open('foo.txt', 'r')
>>> f.readlines()
['This is the first line of the file.\n', 'Second line of the file\n']

读取文件行的另一种方法是直接循环遍历文件对象。这种方法节省内存,速度快,而且代码更简单:

>>> for line in f:
        print line,

This is the first line of the file.
Second line of the file

撰写回答