Skip to content

Instantly share code, notes, and snippets.

@fernandojsg
Last active August 29, 2015 14:21
Show Gist options
  • Save fernandojsg/9fcbc50fab7b2f84660d to your computer and use it in GitHub Desktop.
Save fernandojsg/9fcbc50fab7b2f84660d to your computer and use it in GitHub Desktop.
Available and used space in a folder (Python)
import math
def convertSize(size):
size_name = ("B","KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size,1024)))
p = math.pow(1024,i)
s = round(size/p,2)
if (s > 0):
return '%s %s' % (s,size_name[i])
else:
return '0B'
import os
f = os.statvfs("/srv/storage_volumes/data01")
total = f.f_frsize*f.f_blocks
free = f.f_frsize*f.f_bfree
available = f.f_frsize*f.f_bavail
used = total - free
usedperc = float(used) / total
print "Total: " + convertSize(total)
print "Available: " + convertSize(available)
print "%% Used: %.2f%%" % (usedperc*100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment