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
On Sat, Jun 11, 2011 at 11:51 AM, Giampaolo Rodolà g.rodola@gmail.comwrote:
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
On Sat, Jun 11, 2011 at 13:51, Giampaolo Rodolà g.rodola@gmail.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.
On Sat, 11 Jun 2011 21:51:56 +0300, Giampaolo Rodolà g.rodola-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:
The posix implementation is nothing but a wrapper around os.statvfs():
[...]
The Windows implementation requires GetDiskFreeSpaceEx() [...]
I'd suggest mentioning in the documentation that symbolic links are resolved for the supplied path. I know that's fact for GetDiskFreeSpaceEx() and a quick look suggests it's also the case for statvfs(). It would be nice to be aware of this and the fact that it works the same on both platforms.
This is now tracked at http://bugs.python.org/issue12442
Regards,
--- Giampaolo http://code.google.com/p/pyftpdlib/ http://code.google.com/p/psutil/