Find free disk space in python on OS/X

Evert picture Evert · Apr 25, 2009 · Viewed 32.4k times · Source

I'm looking for the number of free bytes on my HD, but have trouble doing so on python.

I've tried the following:

import os

stat = os.statvfs(path)
print stat.f_bsize * stat.f_bavail

But, on OS/X it gives me a 17529020874752 bytes, which is about about 1.6 TB, which would be very nice, but unfortunately not really true.

What's the best way to get to this figure?

Answer

Nicholas Riley picture Nicholas Riley · Apr 25, 2009

Try using f_frsize instead of f_bsize.

>>> s = os.statvfs('/')
>>> (s.f_bavail * s.f_frsize) / 1024
23836592L
>>> os.system('df -k /')
Filesystem   1024-blocks     Used Available Capacity  Mounted on
/dev/disk0s2   116884912 92792320  23836592    80%    /