这是可能的,以确定有多少行中的文件不存在,每行迭代?这是、有多少、不存在、迭代

2023-09-11 06:16:19 作者:两粒种子,一片森林。

可能重复:   如何获得线在Python便宜算什么?

美好的一天。我有下面的一些code,每行文件读取和反迭代它实现了。

Good day. i have some code below, which implements per line file reading and counter iteration.

def __set_quantity_filled_lines_in_file(self):
    count = 0
    with open(self.filename, 'r') as f:
        for line in f:
             count += 1
    return count

我的问题是,有没有方法来确定有多少文本数据在当前文件中的行,而不每行迭代?

My question is, are there methods to determine how many lines of text data in current file without per line iteration?

谢谢!

推荐答案

一般的它不可能做的比读每一个字符的文件中,并计算换行符更好。

In general it's not possible to do better than reading every character in the file and counting newline characters.

有可能是可能的,如果你知道该文件的内部结构的细节。例如,如果文件是1024KB长,且每一行是1kB的长度,则可以推断出有该文件中1024行

It may be possible if you know details about the internal structure of the file. For example, if the file is 1024kB long, and every line is 1kB in length, then you can deduce there are 1024 lines in the file.