Advertisement
steve-shambles-2109

169-Report on Disk Usage

Oct 15th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. """
  2. Python code snippets vol 34:
  3. 169-Report on Disk Usage
  4. stevepython.wordpress.com
  5.  
  6. this does a similar job to snippet 75 but lot more info
  7.  
  8. Tested on: Win 7\Linux Mint 19.1
  9.  
  10. pip3 install psutil
  11.  
  12. source:
  13. https://github.com/giampaolo/psutil/blob/master/scripts/disk_usage.py
  14. """
  15.  
  16. import sys
  17. import os
  18. import psutil
  19. from psutil._common import bytes2human
  20.  
  21. def main():
  22.     '''Report on Disk Usage'''
  23.     templ = "%-17s %8s %8s %8s %5s%% %9s  %s"
  24.     print(templ % ("Device", "Total", "Used", "Free", "Use ", "Type",
  25.                    "Mount"))
  26.     for part in psutil.disk_partitions(all=False):
  27.         if os.name == 'nt':
  28.             if 'cdrom' in part.opts or part.fstype == '':
  29.                 # skip cd-rom drives with no disk in it; they may raise
  30.                 # ENOENT, pop-up a Windows GUI error for a non-ready
  31.                 # partition or just hang.
  32.                 continue
  33.         usage = psutil.disk_usage(part.mountpoint)
  34.         print(templ % (
  35.             part.device,
  36.             bytes2human(usage.total),
  37.             bytes2human(usage.used),
  38.             bytes2human(usage.free),
  39.             int(usage.percent),
  40.             part.fstype,
  41.             part.mountpoint))
  42.  
  43.  
  44. if __name__ == '__main__':
  45.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement