Monday, September 21, 2015

How to get disk usage by Python in Linux

To get disk usage,

you can use the following code:

import os

statvfs = os.statvfs('/')

total_disk_space = statvfs.f_frsize * statvfs.f_blocks
free_disk_space = statvfs.f_frsize * statvfs.f_bfree
disk_usage = (total_disk_space - free_disk_space) * 100.0 / total_disk_space

print total_disk_space
print free_disk_space
print disk_usage

Reference:
http://stackoverflow.com/questions/4260116/find-size-and-free-space-of-the-filesystem-containing-a-given-file

No comments:

Post a Comment