[Python-ideas] Adding shutil.disk_usage()

Giampaolo Rodolà g.rodola at gmail.com
Sat Jun 11 20:51:56 CEST 2011


Hi all,
I've just implemented this functionality in psutil for both POSIX and
Windows and thought it might be nice to have it in shutil module as
well since it's useful when doing system monitoring:
http://code.google.com/p/psutil/issues/detail?id=172

The posix implementation is nothing but a wrapper around os.statvfs():

def disk_usage(path):
    """Return disk usage associated with path."""
    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
    percent = (float(used) / total) * 100
    # NB: the percentage is -5% than what shown by df due to
    # reserved blocks that we are currently not considering:
    # http://goo.gl/sWGbH
    return ntuple_diskinfo(total, used, free, round(percent, 1))

...and reflects what returned by "df /somepath".
The Windows implementation requires GetDiskFreeSpaceEx() which is not
exposed in python stdlib but can be added as a privade module
(Modules/_winutil.c maybe?) or retrieved via ctypes.

Thoughts?


--- Giampaolo
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil



More information about the Python-ideas mailing list