2

ディレクトリのサイズを Unix と Python で比較すると、結果が少し異なります (「ディスク使用量」で 5% 小さくなります)。なんで ?(すべてのサブフォルダーは読み取り可能です。Mac OSX Mountain lion、python バージョン 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

ご回答ありがとうございます。

エリック。

4

1 に答える 1