25

HD の空きバイト数を探していますが、Python ではうまくいきません。

私は次のことを試しました:

import os

stat = os.statvfs(path)
print stat.f_bsize * stat.f_bavail

しかし、OS/X では 17529020874752 バイトが得られます。これは約 1.6 TB です。これは非常に良いことですが、残念ながら実際にはそうではありません。

この数字に到達するための最良の方法は何ですか?

4

7 に答える 7

38

f_frsizeの代わりに使ってみてくださいf_bsize

>>> s = os.statvfs('/')
>>> (s.f_bavail * s.f_frsize) / 1024
23836592L
>>> os.system('df -k /')
Filesystem   1024-blocks     Used Available Capacity  Mounted on
/dev/disk0s2   116884912 92792320  23836592    80%    /
于 2009-04-24T22:42:45.450 に答える
19

UNIXの場合:

import os
from collections import namedtuple

_ntuple_diskusage = namedtuple('usage', 'total used free')

def disk_usage(path):
    """Return disk usage statistics about the given path.

    Returned valus is a named tuple with attributes 'total', 'used' and
    'free', which are the amount of total, used and free space, in bytes.
    """
    st = os.statvfs(path)
    free = st.f_bavail * st.f_frsize
    total = st.f_blocks * st.f_frsize
    used = (st.f_blocks - st.f_bfree) * st.f_frsize
    return _ntuple_diskusage(total, used, free)

使用法:

>>> disk_usage('/')
usage(total=21378641920, used=7650934784, free=12641718272)
>>>

Windowsの場合、psutilを使用できます。

于 2011-09-02T15:10:15.893 に答える
15

Python 3.3 以降では、 shutil が同じ機能を提供します

>>> import shutil
>>> shutil.disk_usage("/")
usage(total=488008343552, used=202575314944, free=260620050432)
>>> 
于 2015-07-19T10:57:34.693 に答える
4

Psutil モジュール も使用できます。

>>> psutil.disk_usage('/')
usage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)

ドキュメントはここにあります。

于 2013-03-05T09:25:10.150 に答える
0
def FreeSpace(drive):
    """ Return the FreeSape of a shared drive in bytes"""
    try:
        fso = com.Dispatch("Scripting.FileSystemObject")
        drv = fso.GetDrive(drive)
        return drv.FreeSpace
    except:
        return 0
于 2013-09-25T10:22:28.013 に答える
-1

OS に依存しませんが、これは Linux で動作し、おそらく OS X でも動作します:

print commands.getoutput('df .').split('\n')[1].split()[3]

それはどのように機能しますか?「df」の出力を取得します。このコマンドは、現在のディレクトリが含まれるパーティションに関するディスク情報を提供し、(画面に表示されるのと同じように) 2 行に分割し、その 2 行目を取得します ([1] を最初に split()) を実行し、次にその行を空白で区切られた複数の部分に分割し、最後にそのリストの 4 番目の要素を取得します。

>>> commands.getoutput('df .')
'Filesystem           1K-blocks      Used Available Use% Mounted on\n/dev/sda3             80416836  61324872  15039168  81% /'

>>> commands.getoutput('df .').split('\n')
['Filesystem           1K-blocks      Used Available Use% Mounted on', '/dev/sda3             80416836  61324908  15039132  81% /']

>>> commands.getoutput('df .').split('\n')[1]
'/dev/sda3             80416836  61324908  15039132  81% /'

>>> commands.getoutput('df .').split('\n')[1].split()
['/dev/sda3', '80416836', '61324912', '15039128', '81%', '/']

>>> commands.getoutput('df .').split('\n')[1].split()[3]
'15039128'

>>> print commands.getoutput('df .').split('\n')[1].split()[3]
15039128
于 2009-04-25T05:19:24.390 に答える
-5

どうしたの

import subprocess
proc= subprocess.Popen( "df", stdout=subprocess.PIPE )
proc.stdout.read()
proc.wait()
于 2009-04-24T22:34:47.607 に答える