Calculate OS Size with Python

df.py
#! /usr/bin/python
# my first python attempt.
# Mark Young 14 July 2016
import sys
import os
import subprocess
def reportUsage(label, total, free):
  used = total - free
  print "%s=%dG, used=%dG (%d%%)" % (label, free, used, used*100/total)
p = subprocess.Popen(["df", "-k", "."], stdout=subprocess.PIPE)
print p.stdout.read()
rc = p.wait()
stat = os.statvfs(".")
total = (stat.f_bsize * stat.f_blocks) / 1024 / 1024/1024
free = (stat.f_bsize * stat.f_bfree) / 1024 / 1024/1024
avail = (stat.f_bsize * stat.f_bavail) / 1024 / 1024/1024
print "Total=%dG" % total
reportUsage("Free", total, free)
reportUsage("Avail", total, avail)

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.