| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 """ | |
| 4 Return disk usage statistics about the given path as a (total, used, free) | |
| 5 namedtuple. Values are expressed in bytes. | |
| 6 """ | |
| 7 # Author: Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com> | |
| 8 # License: MIT | |
| 9 | |
| 10 import os | |
| 11 import collections | |
| 12 | |
| 13 _ntuple_diskusage = collections.namedtuple('usage', 'total used free') | |
| 14 | |
| 15 if hasattr(os, 'statvfs'): # POSIX | |
| 16 def disk_usage(path): | |
| 17 st = os.statvfs(path) | |
| 18 free = st.f_bavail * st.f_frsize | |
| 19 total = st.f_blocks * st.f_frsize | |
| 20 used = (st.f_blocks - st.f_bfree) * st.f_frsize | |
| 21 return _ntuple_diskusage(total, used, free) | |
| 22 | |
| 23 elif os.name == 'nt': # Windows | |
| 24 import ctypes | |
| 25 import sys | |
| 26 | |
| 27 def disk_usage(path): | |
| 28 _, total, free = ctypes.c_ulonglong(), ctypes.c_ulonglong(), \ | |
| 29 ctypes.c_ulonglong() | |
| 30 if sys.version_info >= (3,) or isinstance(path, unicode): | |
| 31 fun = ctypes.windll.kernel32.GetDiskFreeSpaceExW | |
| 32 else: | |
| 33 fun = ctypes.windll.kernel32.GetDiskFreeSpaceExA | |
| 34 ret = fun(path, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free)
) | |
| 35 if ret == 0: | |
| 36 raise ctypes.WinError() | |
| 37 used = total.value - free.value | |
| 38 return _ntuple_diskusage(total.value, used, free.value) | |
| 39 else: | |
| 40 raise NotImplementedError("platform not supported") | |
| 41 | |
| 42 disk_usage.__doc__ = __doc__ | |
| 43 | |
| 44 if __name__ == '__main__': | |
| 45 print disk_usage(os.getcwd()) | |
| OLD | NEW |