Cross-platform space remaining on volume using python
<p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>

<pre><code>import ctypes
import os
import platform
import sys

def get_free_space_mb(dirname):
    """Return folder/drive free space (in megabytes)."""
    if platform.system() == 'Windows':
        free_bytes = ctypes.c_ulonglong(0)
        ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dirname), None, None, ctypes.pointer(free_bytes))
        return free_bytes.value / 1024 / 1024
    else:
        st = os.statvfs(dirname)
        return st.f_bavail * st.f_frsize / 1024 / 1024
</code></pre>

<p>Note that you <em>must</em> pass a directory name for <code>GetDiskFreeSpaceEx()</code> to work
(<code>statvfs()</code> works on both files and directories). You can get a directory name
from a file with <code>os.path.dirname()</code>.</p>

<p>Also see the documentation for <a href="https://docs.python.org/3/library/os.html#os.fstatvfs"><code>os.statvfs()</code></a> and <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx"><code>GetDiskFreeSpaceEx</code></a>.</p>
