On Sat, Jun 11, 2011 at 11:51 AM, Giampaolo Rodolà
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?
Makes sense to me. Though I would personally leave the percent calculation up to the caller or at least leave the rounding to the caller. I'll leave opinion on which implementation to use on windows up to someone more familiar with that platform. Attach your patch(es) implementing it to a feature request issue on bugs.python.org. -gps