Python:os.stat().st_size提供的值与du不同 [英] Python: os.stat().st_size gives different value than du
问题描述
我正在创建一个实用程序,它将遍历目录并获取所有目录的子目录和文件的大小并存储值.但是,尺寸计算不正确.
I'm creating a utility that will walk through directories and get the sizes of child directories and files for all directories and store the value. However, the sizes aren't computed correctly.
这是我的课程,它自动遍历所有子目录:
Here's my class, which automatically recurses through all sub-directories:
class directory:
'''
Class that automatically traverses directories
and builds a tree with size info
'''
def __init__(self, path, parent=None):
if path[-1] != '/':
# Add trailing /
self.path = path + '/'
else:
self.path = path
self.size = 4096
self.parent = parent
self.children = []
self.errors = []
for i in os.listdir(self.path):
try:
self.size += os.lstat(self.path + i).st_size
if os.path.isdir(self.path + i) and not os.path.islink(self.path + i):
a = directory(self.path + i, self)
self.size += a.size
self.children.append(a)
except OSError:
self.errors.append(path + i)
我有一个用于测试该程序的视频目录:
I have a directory of videos that I'm testing this program with:
>>> a = directory('/var/media/television/The Wire')
>>> a.size
45289964053
但是,当我与du尝试相同时,我得到了
However, when I try the same with du, I get
$ du -sx /var/media/television/The\ Wire
44228824
目录不包含任何链接或任何特殊内容.
The directories don't contain any links or anything special.
有人可以解释为什么 os.stat()
给出奇怪的尺寸读数吗?
Could someone explain why os.stat()
is giving weird size readings?
平台:
- Linux(Fedora 13)
- Python 2.7
推荐答案
将此文件视为foo
-rw-rw-r-- 1 unutbu unutbu 25334 2010-10-31 12:55 foo
它由25334个字节组成.
It consists of 25334 bytes.
tune2fs告诉我foo驻留在块大小为4096字节的文件系统上:
tune2fs tells me foo resides on a filesystem with block size 4096 bytes:
% sudo tune2fs -l /dev/mapper/vg1-OS1
...
Block size: 4096
...
因此,即使文件系统的内容仅由1个字节组成,它在文件系统上的最小文件也将占用4096个字节.随着文件的增大,将以4096字节的块分配空间.
Thus, the smallest file on the filesystem will occupy 4096 bytes, even if its contents consist of just 1 byte. As the file grows larger, space is allocated in 4096-byte blocks.
du报告
% du -B1 foo
28672 foo
请注意28672/4096 =7.这意味着foo占用了文件系统上的7个4096字节块.这是容纳25334个字节所需的最小块数.
Note that 28672/4096 = 7. This is saying that foo occupys 7 4096-byte blocks on the filesystem. This is the smallest number of blocks needed to hold 25334 bytes.
% du foo
28 foo
此版本的 du
仅报告28672/1024的四舍五入.
This version of du
is just reporting 28672/1024 rounded down.
这篇关于Python:os.stat().st_size提供的值与du不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!