为什么 Python 中没有 len(file) ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16865390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:51:52  来源:igfitidea点击:

Why no len(file) in Python?

python

提问by Dr. Kickass

I'm not exactly new to Python, but I do still have trouble understanding what makes something "Pythonic" (and the converse).

我对 Python 并不完全陌生,但我仍然无法理解什么是“Pythonic”(反之亦然)。

So forgive me if this is a stupid question, but why can't I get the size of a file by doing a len(file)?

如果这是一个愚蠢的问题,请原谅我,但是为什么我不能通过执行 len(file) 来获取文件的大小?

file.__len__is not even implemented, so it's not like it's needed for something else? Would it be confusing/inconsistent for some reason if it was implemented to return the file size?

文件。__len__甚至没有实现,所以它不像其他东西需要它?如果它被实现为返回文件大小,它会因为某种原因而混淆/不一致吗?

采纳答案by John La Rooy

file is an iterator. To find the number of linesyou need to read the entire file

文件是一个迭代器。要查找的号码线需要读取整个文件

sum(1 for line in file)

if you want the number of bytesin a file, use os.stat

如果您想要文件中的字节数,请使用os.stat

eg

例如

import os
os.stat(filename).st_size

回答by wardd

I would say because finding the length depends on OS specific functionality. You can find the length of a file with this code:

我会说因为找到长度取决于操作系统特定的功能。您可以使用以下代码找到文件的长度:

import os os.path.getsize('C:\\file.txt')

import os os.path.getsize('C:\\file.txt')

You could also read the entire file into a string and find the length of the string. However you would want to be sure that the file is not of a huge size that will eat up all your memory.

您还可以将整个文件读入一个字符串并找到该字符串的长度。但是,您需要确保该文件的大小不会占用您所有的内存。

回答by Ashwini Chaudhary

filereturns an iterator, so you can't use len()on it.

file返回一个迭代器,所以你不能使用len()它。

To get the size of a file you can use os.stat:

要获取文件的大小,您可以使用os.stat

>>> foo = os.stat("abc")
>>> foo.st_size
193L

If by size you mean number of line then try these:

如果按大小表示行数,请尝试以下操作:

len(open("abc").readlines())

or

或者

sum (1 for _ in open("abc"))

sum (1 for _ in open("abc"))

回答by Charles Burns

Files have a broader definition, especially in Unix, than you may be thinking. What is the length of a printer, for example? Or a CDROM drive? Both are files in /dev, and sort of in Windows.

文件具有比您想象的更广泛的定义,尤其是在 Unix 中。例如,打印机的长度是多少?还是光驱?两者都是 /dev 中的文件,并且都在 Windows 中。

For what we normally think of as a file, what would its length be? The size of the variable? The size of the file in bytes? The latter makes more sense, but then it gets ickier. Should the size of the file's contents be listed, or its size on disk (modulus allocation unit size). The question arises again for sparse files (files that have large empty sections which take no space, but are part of the file's normally reported size, supported by some file systems like NTFS and XFS).

对于我们通常认为的文件,它的长度是多少?变量的大小?文件的大小(以字节为单位)?后者更有意义,但随后变得更恶心。是否应列出文件内容的大小,或其在磁盘上的大小(模数分配单元大小)。对于稀疏文件(具有大的空白部分的文件,这些文件不占用空间,但属于文件通常报告的大小的一部分,由一些文件系统如 NTFS 和 XFS 支持)再次出现问题。

Of course, the answer to all of those could be, "just pick one and document what you picked." Perhaps that is exactly what should be done, but to be Pythonic, something usually must be clear-cut without having to read a lot of docs. len(string)is mostly obvious (one may ask if bytes or characters are the return value), len(array)is obvious, len(file)maybe not quite enough.

当然,所有这些问题的答案都可能是,“只需选择一个并记录您选择的内容。” 也许这正是应该做的,但要成为 Pythonic,通常必须明确一些,而不必阅读大量文档。len(string)很明显(人们可能会问是字节还是字符是返回值),len(array)很明显,len(file)可能还不够。

回答by Aya

So forgive me if this is a stupid question, but why can't I get the size of a file by doing a len(file)?

如果这是一个愚蠢的问题,请原谅我,但是为什么我不能通过执行 len(file) 来获取文件的大小?

Charles Burns' answer makes a good point about Unix's "everything is a file" philosophy, and, although you always can use os.fstat()to get the 'size' for any file descriptor, with something like...

Charles Burns 的回答很好地说明了 Unix 的“一切都是文件”的理念,而且,尽管您总是可以使用os.fstat()来获取任何文件描述符的“大小”,例如...

import os

f = open(anything)
size = os.fstat(f.fileno()).st_size

...it may not return anything meaningful or useful...

...它可能不会返回任何有意义或有用的东西......

>>> os.fstat(sys.stdout.fileno()).st_size
0
>>> fd1, fd2 = os.pipe()
>>> os.fstat(fd1).st_size
0

I think the reason is that a Python file object, or file-like object, is supposed to represent a stream, and streams don't inherently have a length, especially if they're write-only, like sys.stdout.

我认为原因是 Python 文件对象或类似文件的对象应该表示一个流,而流本身没有长度,特别是如果它们是只写的,例如sys.stdout.

Usually, the only thing you canguarantee about a Python file-like object is that it will support at least one of read()or write(), and that's about it.

通常,对于Python 文件类对象,您唯一可以保证的是它至少支持read()或 之一write(),仅此而已。

回答by gepoch

A simple way to measure the number of characters would be:

测量字符数的一种简单方法是:

file = open('file.bin', 'r')
# Seek to the end. (0 bytes relative to the end)
file.seek(0, 2)
length = file.tell()