[英]File size discrepancy between du command and Python function
I have a script I run nightly to get the ammt of stuff stored in a specific directory on my server. 我有一个脚本,每晚运行一次,以将大量内容存储在服务器上的特定目录中。 This is the function I am using for that core part:
这是我用于该核心部分的功能:
def get_size(start_path = '.'):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
try:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
print str(total_size)+" bytes / "+str(size(total_size))+" counted"+" <------------ current position: "+start_path+" : "+f
for location in locations_dict:
if locations_dict[location][1] != "":
print str(location)+": "+str(size(locations_dict[location][1]))
except OSError, e:
print e
return total_size
For some reason, I am getting a different value when I manually run 由于某些原因,当我手动运行时,我得到了一个不同的值
$ du -hc [path to dir]
With the Python I get 20551043874445 bytes (converts to 20.5 TB). 使用Python,我得到20551043874445字节(转换为20.5 TB)。 With
du
I get 28 TB (I am re-running now without -h
to get the value in bytes). 使用
du
可以获得28 TB(我现在不带-h
重新运行以获取字节值)。
Clearly that Python function is missing something, but I'm not sure what or how. 显然,Python函数缺少某些内容,但是我不确定是什么或如何。 Any ideas?
有任何想法吗?
du
shows the size in 512-byte blocks. du
以512字节块为单位显示大小。 If the file size isn't a multiple of 512, du
rounds up. 如果文件大小不是512的倍数,则
du
向上取整。 To get the equivalent value in Python, instead of using os.path.getsize()
, use os.stat()
and use the st_blocks
attribute of the result. 要在Python中获得等效值,请使用
os.stat()
并使用结果的st_blocks
属性,而不要使用os.path.getsize()
。
total_size += os.stat(fp).st_blocks * 512;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.