On Sat, Jun 11, 2011 at 13:51, Giampaolo Rodolà email@example.com wrote:
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.
The GetDiskFreeSpaceEx call should just be exposed within Modules/posixmodule.c. See the posix__getfinalpathname function for an example.