Cross-platform space remaining on volume using python

PythonWindowsLinuxMacosDiskspace

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

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

Also see the documentation for os.statvfs() and GetDiskFreeSpaceEx.

Solution 2 - Python

Install psutil using pip install psutil. Then you can get the amount of free space in bytes using:

import psutil
print(psutil.disk_usage(".").free)

Solution 3 - Python

You could use the wmi module for windows and os.statvfs for unix

for window

import wmi

c = wmi.WMI ()
for d in c.Win32_LogicalDisk():
    print( d.Caption, d.FreeSpace, d.Size, d.DriveType)

for unix or linux

from os import statvfs

statvfs(path)

Solution 4 - Python

If you're running python3:

Using shutil.disk_usage()with os.path.realpath('/') name-regularization works:

from os import path
from shutil import disk_usage

print([i / 1000000 for i in disk_usage(path.realpath('/'))])

Or

total_bytes, used_bytes, free_bytes = disk_usage(path.realpath('D:\\Users\\phannypack'))

print(total_bytes / 1000000) # for Mb
print(used_bytes / 1000000)
print(free_bytes / 1000000)

    

giving you the total, used, & free space in MB.

Solution 5 - Python

If you dont like to add another dependency you can for windows use ctypes to call the win32 function call directly.

import ctypes

free_bytes = ctypes.c_ulonglong(0)

ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(u'c:\\'), None, None, ctypes.pointer(free_bytes))

if free_bytes.value == 0:
   print 'dont panic'

Solution 6 - Python

From Python 3.3 you can use shutil.disk_usage("/").free from standard library for both Windows and UNIX :)

Solution 7 - Python

A good cross-platform way is using psutil: http://pythonhosted.org/psutil/#disks (Note that you'll need psutil 0.3.0 or above).

Solution 8 - Python

You can use df as a cross-platform way. It is a part of GNU core utilities. These are the core utilities which are expected to exist on every operating system. However, they are not installed on Windows by default (Here, GetGnuWin32 comes in handy).

df is a command-line utility, therefore a wrapper required for scripting purposes. For example:

from subprocess import PIPE, Popen

def free_volume(filename):
    """Find amount of disk space available to the current user (in bytes) 
       on the file system containing filename."""
    stats = Popen(["df", "-Pk", filename], stdout=PIPE).communicate()[0]
    return int(stats.splitlines()[1].split()[3]) * 1024

Solution 9 - Python

The os.statvfs() 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).

Otherwise, you can use the pywin32 library to directly call the GetDiskFreeSpaceEx function.

Solution 10 - Python

Below code returns correct value on windows

import win32file    

def get_free_space(dirname):
    secsPerClus, bytesPerSec, nFreeClus, totClus = win32file.GetDiskFreeSpace(dirname)
    return secsPerClus * bytesPerSec * nFreeClus

Solution 11 - Python

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.

For Windows, there's the GetDiskFreeSpaceEx method in the win32 extensions.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustin WaddellView Question on Stackoverflow
Solution 1 - PythonFrankovskyi BogdanView Answer on Stackoverflow
Solution 2 - PythonjhasseView Answer on Stackoverflow
Solution 3 - PythonErxinView Answer on Stackoverflow
Solution 4 - PythonRob TruxalView Answer on Stackoverflow
Solution 5 - PythonStefan LundströmView Answer on Stackoverflow
Solution 6 - PythonMišoView Answer on Stackoverflow
Solution 7 - PythonYuriyView Answer on Stackoverflow
Solution 8 - PythonjfsView Answer on Stackoverflow
Solution 9 - PythonGreg HewgillView Answer on Stackoverflow
Solution 10 - PythonSanjay BhosaleView Answer on Stackoverflow
Solution 11 - PythonhassegView Answer on Stackoverflow