michael@0: #!/usr/bin/env python michael@0: michael@0: # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: """ michael@0: List all mounted disk partitions a-la "df -h" command. michael@0: """ michael@0: michael@0: import sys michael@0: import os michael@0: import psutil michael@0: from psutil._compat import print_ michael@0: michael@0: def bytes2human(n): michael@0: # http://code.activestate.com/recipes/578019 michael@0: # >>> bytes2human(10000) michael@0: # '9.8K' michael@0: # >>> bytes2human(100001221) michael@0: # '95.4M' michael@0: symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') michael@0: prefix = {} michael@0: for i, s in enumerate(symbols): michael@0: prefix[s] = 1 << (i+1)*10 michael@0: for s in reversed(symbols): michael@0: if n >= prefix[s]: michael@0: value = float(n) / prefix[s] michael@0: return '%.1f%s' % (value, s) michael@0: return "%sB" % n michael@0: michael@0: michael@0: def main(): michael@0: templ = "%-17s %8s %8s %8s %5s%% %9s %s" michael@0: print_(templ % ("Device", "Total", "Used", "Free", "Use ", "Type", "Mount")) michael@0: for part in psutil.disk_partitions(all=False): michael@0: if os.name == 'nt' and 'cdrom' in part.opts: michael@0: # may raise ENOENT if there's no cd-rom in the drive michael@0: continue michael@0: usage = psutil.disk_usage(part.mountpoint) michael@0: print_(templ % (part.device, michael@0: bytes2human(usage.total), michael@0: bytes2human(usage.used), michael@0: bytes2human(usage.free), michael@0: int(usage.percent), michael@0: part.fstype, michael@0: part.mountpoint)) michael@0: michael@0: if __name__ == '__main__': michael@0: sys.exit(main())