[英]File size discrepancy between du command and Python function
我有一个脚本,每晚运行一次,以将大量内容存储在服务器上的特定目录中。 这是我用于该核心部分的功能:
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
由于某些原因,当我手动运行时,我得到了一个不同的值
$ du -hc [path to dir]
使用Python,我得到20551043874445字节(转换为20.5 TB)。 使用du
可以获得28 TB(我现在不带-h
重新运行以获取字节值)。
显然,Python函数缺少某些内容,但是我不确定是什么或如何。 有任何想法吗?
du
以512字节块为单位显示大小。 如果文件大小不是512的倍数,则du
向上取整。 要在Python中获得等效值,请使用os.stat()
并使用结果的st_blocks
属性,而不要使用os.path.getsize()
。
total_size += os.stat(fp).st_blocks * 512;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.