Mercurial > cpython-lru_cache-weakref
diff Lib/shutil.py @ 71110:2fc102ebaf73
Issue #12442: add shutil.disk_usage()
| author | Giampaolo Rodola' <g.rodola@gmail.com> |
|---|---|
| date | Fri, 01 Jul 2011 13:55:36 +0200 |
| parents | f5aa0e52dd81 |
| children | f1e761dea2ee |
line wrap: on
line diff
--- a/Lib/shutil.py Fri Jul 01 13:47:03 2011 +0200 +++ b/Lib/shutil.py Fri Jul 01 13:55:36 2011 +0200 @@ -12,6 +12,7 @@ import collections import errno import tarfile +from collections import namedtuple try: import bz2 @@ -754,3 +755,21 @@ func = _UNPACK_FORMATS[format][1] kwargs = dict(_UNPACK_FORMATS[format][2]) func(filename, extract_dir, **kwargs) + +if hasattr(os, "statvfs") or os.name == 'nt': + _ntuple_diskusage = namedtuple('usage', 'total used free') + + def disk_usage(path): + """Return disk usage statistics about the given path as a namedtuple + including total, used and free space expressed in bytes. + """ + if hasattr(os, "statvfs"): + 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 + else: + import nt + total, free = nt._getdiskusage(path) + used = total - free + return _ntuple_diskusage(total, used, free)
