diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /python/psutil/examples/disk_usage.py | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'python/psutil/examples/disk_usage.py')
-rwxr-xr-x | python/psutil/examples/disk_usage.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/python/psutil/examples/disk_usage.py b/python/psutil/examples/disk_usage.py new file mode 100755 index 000000000..d8600a8c4 --- /dev/null +++ b/python/psutil/examples/disk_usage.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +List all mounted disk partitions a-la "df -h" command. + +$ python examples/disk_usage.py +Device Total Used Free Use % Type Mount +/dev/sdb3 18.9G 14.7G 3.3G 77% ext4 / +/dev/sda6 345.9G 83.8G 244.5G 24% ext4 /home +/dev/sda1 296.0M 43.1M 252.9M 14% vfat /boot/efi +/dev/sda2 600.0M 312.4M 287.6M 52% fuseblk /media/Recovery +""" + +import sys +import os +import psutil + + +def bytes2human(n): + # http://code.activestate.com/recipes/578019 + # >>> bytes2human(10000) + # '9.8K' + # >>> bytes2human(100001221) + # '95.4M' + symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') + prefix = {} + for i, s in enumerate(symbols): + prefix[s] = 1 << (i + 1) * 10 + for s in reversed(symbols): + if n >= prefix[s]: + value = float(n) / prefix[s] + return '%.1f%s' % (value, s) + return "%sB" % n + + +def main(): + templ = "%-17s %8s %8s %8s %5s%% %9s %s" + print(templ % ("Device", "Total", "Used", "Free", "Use ", "Type", + "Mount")) + for part in psutil.disk_partitions(all=False): + if os.name == 'nt': + if 'cdrom' in part.opts or part.fstype == '': + # skip cd-rom drives with no disk in it; they may raise + # ENOENT, pop-up a Windows GUI error for a non-ready + # partition or just hang. + continue + usage = psutil.disk_usage(part.mountpoint) + print(templ % ( + part.device, + bytes2human(usage.total), + bytes2human(usage.used), + bytes2human(usage.free), + int(usage.percent), + part.fstype, + part.mountpoint)) + +if __name__ == '__main__': + sys.exit(main()) |