获取文件大小的人类可读版本
从字节大小返回人类可读大小的函数:
>>> human_readable(2048)
'2 kilobytes'
我该怎么做?
A function to return a human-readable size from the bytes size:
>>> human_readable(2048)
'2 kilobytes'
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
通过简单的实现(使用 f 字符串,因此 Python 3.6+)解决上述“任务太小,不需要库”问题:
支持:
示例:
通过 弗雷德·西雷拉
Addressing the above "too small a task to require a library" issue by a straightforward implementation (using f-strings, so Python 3.6+):
Supports:
Example:
by Fred Cirera
具有您正在寻找的所有功能的库是
humanize
。humanize.naturalsize() 似乎可以完成您正在寻找的一切。
示例代码 (Python3.10)
输出
A library that has all the functionality that it seems you're looking for is
humanize
.humanize.naturalsize()
seems to do everything you're looking for.Example code (Python 3.10)
Output
在我看来,以下内容适用于 Python 3.6+,是这里最容易理解的答案,并且允许您自定义使用的小数位数。
The following works in Python 3.6+, is, in my opinion, the easiest to understand answer on here, and lets you customize the amount of decimal places used.
总得有这样的人之一。 好吧,今天是我。 这是一行——如果算上函数签名的话,可以是两行。
如果您需要的大小大于艾字节,那就有点粗糙了:
There's always got to be one of those guys. Well today it's me. Here's a one-liner -- or two lines if you count the function signature.
If you need sizes bigger than an Exabyte, it's a little bit more gnarly:
这是我的版本。 它不使用for循环。 它具有恒定的复杂度 O(1),并且理论上比此处使用 for 循环的答案更有效。
为了更清楚地了解发生了什么,我们可以省略字符串格式化的代码。 以下是实际完成工作的几行代码:
Here's my version. It does not use a for loop. It has constant complexity, O(1), and is in theory more efficient than the answers here that use a for loop.
To make it more clear what is going on, we can omit the code for the string formatting. Here are the lines that actually do the work:
我最近提出了一个避免循环的版本,使用 log2 来确定大小顺序,该顺序兼作后缀列表中的移位和索引:
不过,由于其可读性,它很可能被认为是非Pythonic的。
I recently came up with a version that avoids loops, using
log2
to determine the size order which doubles as a shift and an index into the suffix list:It could well be considered unpythonic for its readability, though.
如果您使用的是 Django,还可以尝试 filesizeformat:
If you're using Django, you can also try filesizeformat:
你应该使用“人性化”。
You should use "humanize".
其中一个库是 hurry.filesize。
One such library is hurry.filesize.
使用 1000 的幂或 kibibytes 会更加标准友好:
PS 永远不要信任打印的库数千个带有 K(大写)后缀的:)
Using either powers of 1000 or kibibytes would be more standard-friendly:
P.S. Never trust a library that prints thousands with the K (uppercase) suffix :)
这几乎可以满足您在任何情况下的需要,可以使用可选参数进行自定义,并且如您所见,它是相当的自我文档化:
示例输出:
高级自定义:
此代码既是Python 2又是Python 2和Python 2的代码。兼容Python 3。 PEP8 合规性是读者的一项练习。 请记住,输出才是最漂亮的。
更新:
如果您需要数千个逗号,只需应用明显的扩展名即可:
例如:
This will do what you need in almost any situation, is customizable with optional arguments, and as you can see, is pretty much self-documenting:
Example output:
Advanced customizations:
This code is both Python 2 and Python 3 compatible. PEP8 compliance is an exercise for the reader. Remember, it's the output that's pretty.
Update:
If you need thousands commas, just apply the obvious extension:
For example:
HumanFriendly 项目可以帮助解决这个问题。
上面的代码将给出 1KB 作为答案。
示例可以在此处找到。
The HumanFriendly project helps with this.
The above code will give 1KB as the answer.
Examples can be found here.
翻阅作为 rush.filesize() 的替代方案提供的代码片段,这里是一个代码片段,它根据所使用的前缀给出不同的精度数字。 它不像一些片段那么简洁,但我喜欢结果。
Riffing on the snippet provided as an alternative to hurry.filesize(), here is a snippet that gives varying precision numbers based on the prefix used. It isn't as terse as some snippets, but I like the results.
根据之前的所有答案,这是我的看法。 它是一个将文件大小(以字节为单位)存储为整数的对象。 但是当您尝试打印该对象时,您会自动获得人类可读的版本。
Drawing from all the previous answers, here is my take on it. It's an object which will store the file size in bytes as an integer. But when you try to print the object, you automatically get a human readable version.
现代 Django 有自我模板标签
filesizeformat
:将值格式化为
人类可读
文件大小(即“13 KB”、“4.1 MB”、“102 字节”等) .)。例如:
如果值为 123456789,则输出将为 117.7 MB。
更多信息: https://docs.djangoproject.com/en/ 1.10/ref/templates/builtins/#filesizeformat
Modern Django have self template tag
filesizeformat
:Formats the value like a
human-readable
file size (i.e. '13 KB', '4.1 MB', '102 bytes', etc.).For example:
If value is 123456789, the output would be 117.7 MB.
More info: https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#filesizeformat
为了以人类可读的形式获取文件大小,我创建了这个函数:
To get the file size in a human readable form, I created this function:
我喜欢 senderle 的十进制版本 的固定精度,所以这是与上面 joctee 的答案的混合(你知道吗您可以使用非整数基数获取日志吗?):
I like the fixed precision of senderle's decimal version, so here's a sort of hybrid of that with joctee's answer above (did you know you could take logs with non-integer bases?):
这是一个 oneliner lambda,没有任何导入来转换为人类可读的文件大小。 传递以字节为单位的值。
Here is an oneliner lambda without any imports to convert to human readable filesize. Pass the value in bytes.
一个简单的 2 衬垫怎么样:
以下是它在底层的工作原理:
Kb
,因此答案应为 X KiB)file_size/value_of_closest_unit
以及单位。但是,如果文件大小为 0 或负数,则它不起作用(因为 log 对于 0 和 -ve 数字未定义)。 您可以为它们添加额外的检查:
示例:
注意 - Kb 和 KiB 之间存在差异。 KB 表示 1000 字节,而 KiB 表示 1024 字节。 KB、MB、GB 都是 1000 的倍数,而 KiB、MiB、GiB 等都是 1024 的倍数。 更多信息请点击这里
How about a simple 2 liner:
Here is how it works under the hood:
Kb
, so the answer should be X KiB)file_size/value_of_closest_unit
along with unit.It however doesn't work if filesize is 0 or negative (because log is undefined for 0 and -ve numbers). You can add extra checks for them:
Examples:
NOTE - There is a difference between Kb and KiB. KB means 1000 bytes, whereas KiB means 1024 bytes. KB,MB,GB are all multiples of 1000, whereas KiB, MiB, GiB etc are all multiples of 1024. More about it here
您在下面找到的绝不是已发布的解决方案中性能最好或最短的解决方案。 相反,它专注于许多其他答案都忽略的一个特定问题。
即当给出像
999_995
这样的输入时的情况:被截断为最接近的整数并应用回输入给出
这似乎正是我们所期望的,直到我们需要控制 <强>输出精度。 这就是事情开始变得有点困难的时候。
将精度设置为 2 位数字后,我们得到:
而不是
1M
。我们如何应对这个问题?
当然,我们可以明确地检查它:
但是我们可以做得更好吗? 在执行最后一步之前,我们能否知道应该以哪种方式切割
订单
?事实证明我们可以。
假设0.5位小数舍入规则,上述
if
条件转换为:导致
给出
What you're about to find below is by no means the most performant or shortest solution among the ones already posted. Instead, it focuses on one particular issue that many of the other answers miss.
Namely the case when input like
999_995
is given:which, being truncated to the nearest integer and applied back to the input gives
This seems to be exactly what we'd expect until we're required to control output precision. And this is when things start to get a bit difficult.
With the precision set to 2 digits we get:
instead of
1M
.How can we counter that?
Of course, we can check for it explicitly:
But can we do better? Can we get to know which way the
order
should be cut before we do the final step?It turns out we can.
Assuming 0.5 decimal rounding rule, the above
if
condition translates into:resulting in
giving
在这里你有它:
最正确的格式:
示例:
Here you have it:
Most correct format:
Examples:
如果有人想知道,请转换 Sridhar Ratnakumar 的回答回到字节,您可以执行以下操作:
用法:
In case someone is wondering, to convert Sridhar Ratnakumar's answer back to bytes you could do the following:
Usage:
如果 Boltons 中提供此功能,这是一个非常好的大多数项目都可以使用的方便库。
This feature if available in Boltons which is a very handy library to have for most projects.
这是我为不同问题写的东西......
很像xApple 的答案,该对象将始终以人类可读的方式打印格式。 不同之处在于它也是一个正确的
int
,因此您可以用它进行数学运算!它将格式说明符直接传递到数字格式并附加后缀,因此几乎可以保证请求的长度将超出两个或三个字符。 我从未使用过这段代码,所以我没有费心去修复它!
用法:
Here's something I wrote for a different question...
Much like xApple's answer, this object will always print in a human-readable format. The difference is that it's also a proper
int
, so you can do math with it!It passes the format specifier straight through to the number format and tacks on the suffix, so it's pretty much guaranteed that the requested length will be exceeded by two or three characters. I've never had a use for this code, so I haven't bothered to fix it!
Usage:
这是 的基于类的变体对于我们这些喜欢强类型的人来说,Fred Cirera 的回答:
Here is a class-based variant of Fred Cirera's answer, for those of us who prefer strong typing:
这是 joctee 实现的替代版本,已更新为使用 Python 3 语法并将否定作为例外情况处理。
我添加了类型和文档,并在收到负数作为输入时引发错误。
下面是一个更简洁的迭代版本:
用法
下面是
format_size
函数的一些用法示例:PyTest
下面是一个测试套件,用于验证
format_size
函数的输出:Here is an alternate version of joctee's implementation updated to use Python 3 syntax and handle negatives as exceptional cases.
I added types and docs, as well as raising an error upon receiving a negative number as input.
Here is a more terse, iterative version:
Usage
Here is some example of usage for the
format_size
function:PyTest
Here is a test suite that verifies the output of the
format_size
function:以下是使用
while
的选项:https://docs. python.org/reference/compound_stmts.html#while
Here is an option using
while
:https://docs.python.org/reference/compound_stmts.html#while
使用。
从naturalsize 模块中
Use
from the naturalsize module.
参考 Sridhar Ratnakumar 的回答,已更新至:
示例输出为:
Referencing Sridhar Ratnakumar's answer, updated to:
and example output is: