我有一个脚本,我每晚运行一次以获取存储在服务器上特定目录中的东西。这是我使用的是核心部分功能:du命令和Python函数之间的文件大小差异
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'措施对磁盘使用量(因此,向上舍入到文件系统块大小),而'os.path.getsize()'返回的文件内容字节大小。但是,如果你的目录包含很多很多的小文件,这只能解释这么大的差别。 –
至少有60,000到100,000个小文件 - 足以说明这一点吗? – dongle