Skip to content

Instantly share code, notes, and snippets.

@yyuu
Created January 6, 2012 06:01
Show Gist options
  • Save yyuu/1569253 to your computer and use it in GitHub Desktop.
Save yyuu/1569253 to your computer and use it in GitHub Desktop.
df in python on Linux
#!/usr/bin/env python
from __future__ import with_statement
import contextlib
import os
import sys
print "Filesystem\tMounted on\tUse%\tIUse%"
with contextlib.closing(open('/etc/mtab')) as fp:
for m in fp:
fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = m.split()
if fs_spec.startswith('/'):
r = os.statvfs(fs_file)
block_usage_pct = 100.0 - (float(r.f_bavail) / float(r.f_blocks) * 100)
inode_usage_pct = 100.0 - (float(r.f_favail) / float(r.f_files) * 100)
print "%s\t%s\t\t%d%%\t%d%%" % (fs_spec, fs_file, block_usage_pct, inode_usage_pct)
# vim:set ft=python :
@tatic0
Copy link

tatic0 commented Nov 28, 2012

Hello,

I really love the code. I'm using it in a simple web interface to monitor a raspberry pi.
I made a few improvements to make it usable as a python module.

Cheers,

Tatic0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment