OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # | |
3 # $Id: disk_usage.py 1143 2011-10-05 19:11:59Z g.rodola $ | |
4 # | |
5 # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. | |
6 # Use of this source code is governed by a BSD-style license that can be | |
7 # found in the LICENSE file. | |
8 | |
9 """ | |
10 List all mounted disk partitions a-la "df -h" command. | |
11 """ | |
12 | |
13 import sys | |
14 import psutil | |
15 | |
16 def convert_bytes(n): | |
17 if n == 0: | |
18 return "0B" | |
19 symbols = ('k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') | |
20 prefix = {} | |
21 for i, s in enumerate(symbols): | |
22 prefix[s] = 1 << (i+1)*10 | |
23 for s in reversed(symbols): | |
24 if n >= prefix[s]: | |
25 value = float(n) / prefix[s] | |
26 return '%.1f%s' % (value, s) | |
27 | |
28 | |
29 def main(): | |
30 templ = "%-17s %8s %8s %8s %5s%% %9s %s" | |
31 print templ % ("Device", "Total", "Used", "Free", "Use ", "Type", "Mount") | |
32 for part in psutil.disk_partitions(all=False): | |
33 usage = psutil.disk_usage(part.mountpoint) | |
34 print templ % (part.device, | |
35 convert_bytes(usage.total), | |
36 convert_bytes(usage.used), | |
37 convert_bytes(usage.free), | |
38 int(usage.percent), | |
39 part.fstype, | |
40 part.mountpoint) | |
41 | |
42 if __name__ == '__main__': | |
43 sys.exit(main()) | |
OLD | NEW |