python get volume free space

Solutions on MaxInterview for python get volume free space by the best coders in the world

showing results for - "python get volume free space"
Leonardo
23 Nov 2019
1import ctypes
2import os
3import platform
4import sys
5
6def get_free_space_mb(dirname):
7    """Return folder/drive free space (in megabytes)."""
8    if platform.system() == 'Windows':
9        free_bytes = ctypes.c_ulonglong(0)
10        ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dirname), None, None, ctypes.pointer(free_bytes))
11        return free_bytes.value / 1024 / 1024
12    else:
13        st = os.statvfs(dirname)
14        return st.f_bavail * st.f_frsize / 1024 / 1024
15