Added better comments and cleaned up by making seperate converting functions and some small error handling.

This commit is contained in:
2017-01-31 21:42:54 +01:00
parent ae634ee4f4
commit 94270a67f4

View File

@@ -3,10 +3,14 @@
# @Author: KevinMidboe
# @Date: 2017-01-28 10:54:06
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-01-28 13:23:07
# @Last Modified time: 2017-01-31 21:40:34
# f_avail = free blocks avail to non sudo
# frsize = fundamental file sys byte size
# f_blocks = total number of blocks in the filesystem
from os import statvfs
def sizeof(num, suffix='B'):
for unit in ['','K','M','G','T','P','E','Z']:
if abs(num) < 1024.0:
@@ -14,13 +18,18 @@ def sizeof(num, suffix='B'):
num /= 1024.0
return "%.1f%s%s" % (num, 'Y', suffix)
# diskUsage uses statvfs to calculate disk size and availability for given disk
def diskUsage(path='/'):
s = statvfs(path)
byteLeft = s.f_bavail * s.f_frsize
byteTotal = s.f_blocks * s.f_frsize
byteUsed = byteTotal-byteLeft
try:
s = statvfs(path)
byteLeft = s.f_bavail * s.f_frsize
byteTotal = s.f_blocks * s.f_frsize
byteUsed = byteTotal-byteLeft
return { 'left':sizeof(byteLeft), 'used':sizeof(byteUsed), 'total':sizeof(byteTotal) }
return { 'left':sizeof(byteLeft), 'used':sizeof(byteUsed), 'total':sizeof(byteTotal) }
except FileNotFoundError:
return None
if __name__=="__main__":
n = diskUsage('/media/hdd1')