Python 获取文件的实际磁盘空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4274899/
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
Get actual disk space of a file
提问by Alex
How do I get the actual filesize on disk in python? (the actual size it takes on the harddrive).
如何在python中获取磁盘上的实际文件大小?(硬盘驱动器上的实际大小)。
采纳答案by ephemient
st = os.stat(…)
du = st.st_blocks * st.st_blksize
回答by TZHX
I'm not certain if this is size on disk, or the logical size:
我不确定这是磁盘大小还是逻辑大小:
import os
filename = "/home/tzhx/stuff.wev"
size = os.path.getsize(filename)
If it's not the droid your looking for, you can round it up by dividing by cluster size (as float), then using ceil, then multiplying.
如果它不是您要找的机器人,您可以通过除以簇大小(作为浮点数),然后使用 ceil,然后乘以四舍五入。
回答by Giampaolo Rodolà
UNIX only:
仅限 UNIX:
import os
from collections import namedtuple
_ntuple_diskusage = namedtuple('usage', 'total used free')
def disk_usage(path):
"""Return disk usage statistics about the given path.
Returned valus is a named tuple with attributes 'total', 'used' and
'free', which are the amount of total, used and free space, in bytes.
"""
st = os.statvfs(path)
free = st.f_bavail * st.f_frsize
total = st.f_blocks * st.f_frsize
used = (st.f_blocks - st.f_bfree) * st.f_frsize
return _ntuple_diskusage(total, used, free)
Usage:
用法:
>>> disk_usage('/')
usage(total=21378641920, used=7650934784, free=12641718272)
>>>
Edit 1 - also for Windows: https://code.activestate.com/recipes/577972-disk-usage/?in=user-4178764
编辑 1 - 也适用于 Windows:https: //code.activestate.com/recipes/577972-disk-usage/?in=user-4178764
Edit 2 - this is also available in Python 3.3+: https://docs.python.org/3/library/shutil.html#shutil.disk_usage
编辑 2 - 这也适用于 Python 3.3+:https://docs.python.org/3/library/shutil.html#shutil.disk_usage
回答by hft
Use os.stat(filename).st_size to get the logical size of the file. Use os.statvfs(filename).f_bsize to get the filesystem block size. Then use integer division to compute the correct size on disk, as below:
使用 os.stat(filename).st_size 获取文件的逻辑大小。使用 os.statvfs(filename).f_bsize 获取文件系统块大小。然后使用整数除法计算磁盘上的正确大小,如下所示:
lSize=os.stat(filename).st_size
bSize=os.statvfs(filename).f_bsize
sizeOnDisk=(lSize/bSize+1)*bSize
回答by Jared Wilber
To get the disk usage for a given file/folder, you can do the following:
要获取给定文件/文件夹的磁盘使用情况,您可以执行以下操作:
import os
def disk_usage(path):
"""Return cumulative number of bytes for a given path."""
# get total usage of current path
total = os.path.getsize(path)
# if path is dir, collect children
if os.path.isdir(path):
for file_name in os.listdir(path):
child = os.path.join(path, file_name)
# recursively get byte use for children
total += disk_usage(child)
return total
The function recursively collects byte usage for files nested within a given path, and returns the cumulative use for the entire path.
You could also add a print "{path}: {bytes}".format(path, total)in there if you want the information for each file to print.
该函数递归地收集嵌套在给定路径中的文件的字节使用情况,并返回整个路径的累积使用情况。print "{path}: {bytes}".format(path, total)如果您希望打印每个文件的信息,您也可以在其中添加一个。
回答by Jonathon Reinhart
Here is the correct way to get a file's size on disk, on platforms where st_blocksis set:
这是在st_blocks设置的平台上在磁盘上获取文件大小的正确方法:
import os
def size_on_disk(path):
st = os.stat(path)
return st.st_blocks * 512
Other answers that indicate to multiply by os.stat(path).st_blksizeor os.vfsstat(path).f_bsizeare simply incorrect.
表示乘以os.stat(path).st_blksize或os.vfsstat(path).f_bsize完全不正确的其他答案。
The Python documentation for os.stat_result.st_blocksvery clearly states:
在Python的文档os.stat_result.st_blocks很清楚地指出:
st_blocks
Number of 512-byte blocks allocated for file. This may be smaller thanst_size/512 when the file has holes.
st_blocks
为文件分配的 512 字节块数。st_size当文件有孔时,这可能小于/512。
Furthermore, the stat(2)man pagesays the same thing:
此外,stat(2)手册页也说了同样的话:
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */

