Using os.statvfs to check disk space
Chris (of Chris’ Random Ramblings) says that os.statvfs is useless by itself because you need to import that statvfs module as well. But I don’t think that’s the case.
According to the Python documentation:
statvfs(path)
Perform a statvfs() system call on the given path. The return value is an object whose attributes describe the filesystem on the given path, and correspond to the members of the statvfs structure, namely: f_frsize, f_blocks, f_bfree, f_bavail, f_files, f_ffree, f_favail, f_flag, f_namemax.
I actually use os.statvfs in my mythcheck.py script that runs from cron to make sure I’m not running out of room on my MythTV box. Here’s the os.statvfs part (with the rest stripped out):
import os
disk = os.statvfs("/")
capacity = disk.f_bsize * disk.f_blocks
available = disk.f_bsize * disk.f_bavail
used = disk.f_bsize * (disk.f_blocks - disk.f_bavail)
In the example above, the values would of course be reported in bytes as given. I end up converting all the values to GB to make the display a little more reasonable. If the disk usage goes over 85% it sends out an email warning so we know that it’s time to delete some shows. Good stuff.
