<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>

[{'answer_id': 51663, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 1, 'selected': False, 'text': '<p>The <a href="https://docs.python.org/2.7/library/os.html" rel="nofollow noreferrer">os.statvfs()</a> function is a better way to get that information for Unix-like platforms (including OS X). The Python documentation says "Availability: Unix" but it\'s worth checking whether it works on Windows too in your build of Python (ie. the docs might not be up to date).</p>\n\n<p>Otherwise, you can use the <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow noreferrer">pywin32</a> library to directly call the <a href="http://msdn.microsoft.com/en-us/library/aa364937(VS.85).aspx" rel="nofollow noreferrer">GetDiskFreeSpaceEx</a> function.</p>\n'}, {'answer_id': 51675, 'author': 'hasseg', 'author_id': 4111, 'author_profile': 'https://Stackoverflow.com/users/4111', 'pm_score': 0, 'selected': False, 'text': '<p>I Don\'t know of any cross-platform way to achieve this, but maybe a good workaround for you would be to write a wrapper class that checks the operating system and uses the best method for each.</p>\n\n<p>For Windows, there\'s the <a href="http://aspn.activestate.com/ASPN/docs/ActivePython/2.2/PyWin32/win32api__GetDiskFreeSpaceEx_meth.html" rel="nofollow noreferrer">GetDiskFreeSpaceEx</a> method in the win32 extensions.</p>\n'}, {'answer_id': 53170, 'author': 'jfs', 'author_id': 4279, 'author_profile': 'https://Stackoverflow.com/users/4279', 'pm_score': 2, 'selected': False, 'text': '<p>You can use <a href="http://man.he.net/?section=all&amp;topic=df" rel="noreferrer">df</a> as a cross-platform way. It is a part of <a href="http://www.gnu.org/software/coreutils/" rel="noreferrer">GNU core utilities</a>. These are the core utilities which are expected to exist on every operating system. However, they are not installed on Windows by default (Here, <a href="http://getgnuwin32.sourceforge.net/" rel="noreferrer">GetGnuWin32</a> comes in handy).</p>\n\n<p><em>df</em> is a command-line utility, therefore a wrapper required for scripting purposes.\nFor example: </p>\n\n<pre><code>from subprocess import PIPE, Popen\n\ndef free_volume(filename):\n    """Find amount of disk space available to the current user (in bytes) \n       on the file system containing filename."""\n    stats = Popen(["df", "-Pk", filename], stdout=PIPE).communicate()[0]\n    return int(stats.splitlines()[1].split()[3]) * 1024\n</code></pre>\n'}, {'answer_id': 1728106, 'author': 'Stefan Lundström', 'author_id': 78757, 'author_profile': 'https://Stackoverflow.com/users/78757', 'pm_score': 3, 'selected': False, 'text': "<p>If you dont like to add another dependency you can for windows use ctypes to call the win32 function call directly. </p>\n\n<pre><code>import ctypes\n\nfree_bytes = ctypes.c_ulonglong(0)\n\nctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(u'c:\\\\'), None, None, ctypes.pointer(free_bytes))\n\nif free_bytes.value == 0:\n   print 'dont panic'\n</code></pre>\n"}, {'answer_id': 2372171, 'author': 'Frankovskyi Bogdan', 'author_id': 273689, 'author_profile': 'https://Stackoverflow.com/users/273689', 'pm_score': 6, 'selected': False, 'text': '<pre><code>import ctypes\nimport os\nimport platform\nimport sys\n\ndef get_free_space_mb(dirname):\n    """Return folder/drive free space (in megabytes)."""\n    if platform.system() == \'Windows\':\n        free_bytes = ctypes.c_ulonglong(0)\n        ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dirname), None, None, ctypes.pointer(free_bytes))\n        return free_bytes.value / 1024 / 1024\n    else:\n        st = os.statvfs(dirname)\n        return st.f_bavail * st.f_frsize / 1024 / 1024\n</code></pre>\n\n<p>Note that you <em>must</em> pass a directory name for <code>GetDiskFreeSpaceEx()</code> to work\n(<code>statvfs()</code> works on both files and directories). You can get a directory name\nfrom a file with <code>os.path.dirname()</code>.</p>\n\n<p>Also see the documentation for <a href="https://docs.python.org/3/library/os.html#os.fstatvfs" rel="noreferrer"><code>os.statvfs()</code></a> and <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx" rel="noreferrer"><code>GetDiskFreeSpaceEx</code></a>.</p>\n'}, {'answer_id': 6773639, 'author': 'Yuriy', 'author_id': 855556, 'author_profile': 'https://Stackoverflow.com/users/855556', 'pm_score': 3, 'selected': False, 'text': '<p>A good cross-platform way is using psutil: <a href="http://pythonhosted.org/psutil/#disks" rel="nofollow">http://pythonhosted.org/psutil/#disks</a>\n(Note that you\'ll need psutil 0.3.0 or above).</p>\n'}, {'answer_id': 19332602, 'author': 'Erxin', 'author_id': 1602830, 'author_profile': 'https://Stackoverflow.com/users/1602830', 'pm_score': 5, 'selected': False, 'text': '<p>You could use the <a href="https://pypi.python.org/pypi/WMI/">wmi</a> module for windows and os.statvfs for unix</p>\n\n<p>for window</p>\n\n<pre><code>import wmi\n\nc = wmi.WMI ()\nfor d in c.Win32_LogicalDisk():\n    print( d.Caption, d.FreeSpace, d.Size, d.DriveType)\n</code></pre>\n\n<p>for unix or linux</p>\n\n<pre><code>from os import statvfs\n\nstatvfs(path)\n</code></pre>\n'}, {'answer_id': 29944093, 'author': 'jhasse', 'author_id': 647898, 'author_profile': 'https://Stackoverflow.com/users/647898', 'pm_score': 5, 'selected': False, 'text': '<p>Install <a href="https://pypi.org/project/psutil/" rel="noreferrer">psutil</a> using <code>pip install psutil</code>. Then you can get the amount of free space in bytes using:</p>\n\n<pre class="lang-python prettyprint-override"><code>import psutil\nprint(psutil.disk_usage(".").free)\n</code></pre>\n'}, {'answer_id': 35860878, 'author': 'Sanjay Bhosale', 'author_id': 1074838, 'author_profile': 'https://Stackoverflow.com/users/1074838', 'pm_score': 1, 'selected': False, 'text': '<p>Below code returns correct value on windows</p>\n\n<pre><code>import win32file    \n\ndef get_free_space(dirname):\n    secsPerClus, bytesPerSec, nFreeClus, totClus = win32file.GetDiskFreeSpace(dirname)\n    return secsPerClus * bytesPerSec * nFreeClus\n</code></pre>\n'}, {'answer_id': 38889608, 'author': 'Mišo', 'author_id': 2988107, 'author_profile': 'https://Stackoverflow.com/users/2988107', 'pm_score': 3, 'selected': False, 'text': '<p>From Python 3.3 you can use <a href="https://docs.python.org/3/library/shutil.html#shutil.disk_usage" rel="noreferrer">shutil.disk_usage("/").free</a> from standard library for both Windows and UNIX :)</p>\n'}, {'answer_id': 48555562, 'author': 'Rob Truxal', 'author_id': 6293857, 'author_profile': 'https://Stackoverflow.com/users/6293857', 'pm_score': 4, 'selected': False, 'text': "<h1>If you're running python3:</h1>\n\n<p>Using <code>shutil.disk_usage()</code>with <code>os.path.realpath('/')</code> name-regularization works:</p>\n\n<pre><code>from os import path\nfrom shutil import disk_usage\n\nprint([i / 1000000 for i in disk_usage(path.realpath('/'))])\n</code></pre>\n\n<p>Or</p>\n\n<pre><code>total_bytes, used_bytes, free_bytes = disk_usage(path.realpath('D:\\\\Users\\\\phannypack'))\n\nprint(total_bytes / 1000000) # for Mb\nprint(used_bytes / 1000000)\nprint(free_bytes / 1000000)\n</code></pre>\n\n<p>giving you the total, used, &amp; free space in MB.</p>\n"}, {'answer_id': 73115043, 'author': 'grepit', 'author_id': 717630, 'author_profile': 'https://Stackoverflow.com/users/717630', 'pm_score': 0, 'selected': False, 'text': '<p>Most previous answers are correct, I\'m using Python 3.10 and shutil.\nMy use case was Windows and C drive only ( but you should be able to extend this for you Linux and Mac as well (here is the <a href="https://docs.python.org/3/library/shutil.html" rel="nofollow noreferrer">documentation</a>)</p>\n<p>Here is the example for Windows:</p>\n<pre><code>import shutil\n\ntotal, used, free = shutil.disk_usage(&quot;C:/&quot;)\n\nprint(&quot;Total: %d GiB&quot; % (total // (2**30)))\nprint(&quot;Used: %d GiB&quot; % (used // (2**30)))\nprint(&quot;Free: %d GiB&quot; % (free // (2**30)))\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51658', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']