Python Mac OS:os.path.getsize返回的值与du -ks不同吗? [英] python Mac OS : os.path.getsize returns different value than du -ks?

查看:301
本文介绍了Python Mac OS:os.path.getsize返回的值与du -ks不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将目录的大小与Unix和python进行比较时,我得到的结果略有不同(磁盘使用率"减小了5%).为什么 ? (我的所有子文件夹都是可读的;我在Mac OSX Mountain Lion(Python 2.7.2版)下工作)

When comparing the size of a directory with Unix and python, I have slightly different results (5% smaller with "disk usage"). Why ? (all my subfolders are readable; I work under Mac OSX Mountain lion, python version 2.7.2)

这是我的代码:

import os, sys
from commands import getstatusoutput

def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size/1024

def get_size2(start_path = '.'):
    cmd = "du -ks "+start_path    # result in blocks of 1024 bytes
    code_err, output = getstatusoutput(cmd)
    return int(output.split()[0])

print get_size()
# 306789
print get_size2()
# 321328

在此先感谢您的回答,

埃里克.

推荐答案

通常,du为您提供数据在磁盘上的存储量,而许多其他测量数据的方法将为您提供数据大小.

In general, du gives you the amount of storage the data is occupying on the disk while a lot of other ways to measure the data will give you the size of the data.

为什么不一样?

  • 有时数据可以非常有效地存储,并且比原始大小需要更少的存储空间.如果您有稀疏文件硬链接,则可能会发生这种情况.尽管这两个在Unix * ish文件系统上很常见,但可能还有其他事情,这取决于文件系统的怪异性.
  • 有时,磁盘上的数据需要的空间比原始大小要大.由于所有文件系统都按块顺序排列其文件数据,而数据并非总是以块大小的倍数出现,因此这是正常现象.这意味着通常浪费最后一块的一部分(即已占用但未使用).
  • Sometimes data can be stored very efficiently and needs less space on storage than its original size. This can happen if you have got sparse files or hard links. While these two are common on Unix*ish file systems there might be other things, depending on the weirdness of your file systems.
  • Sometimes data needs more space on disk than it has originally in size. This is rather normal due to the fact that all file systems order their file data in blocks and data doesn't always come in multiples of the block size. This means that some part of the last block is typically wasted (i. e. occupied but not used).

这篇关于Python Mac OS:os.path.getsize返回的值与du -ks不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆