On Sat, Jun 11, 2011 at 13:51, Giampaolo Rodolą <email@example.com> wrote:
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:
The posix implementation is nothing but a wrapper around os.statvfs():
"""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:
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.