Reputation: 63
When I am printing the following informatiom It looks really ugly. The text display is very long and you cannot really read it.
Code:
import psutil
print("Disk: ", psutil.disk_partitions())
The output I get is:
Disk: [sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='', opts='cdrom'), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='', opts='cdrom'), sdiskpart(device='F:\\', mountpoint='F:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='H:\\', mountpoint='H:\\', fstype='NTFS', opts='rw,removable')]
In one long line! Is there a way to filter the output or display it on multiple lines?
Thanks for helping me :)
Upvotes: 0
Views: 481
Reputation: 19050
psutil.disk_partitinos()
gives you a list of partitions on your systme.
Each element in that list is instance of sdiskpart
which is a namedtuple
with the following properties:
['count', 'device', 'fstype', 'index', 'mountpoint', 'opts']
You will have to process this list and format and display it the way you want using str.format()
and print()
.
Please refer to the psutil documentation.
A simple function that displays "disk information" in a "better way" could be something as simple as:
Example:
from psutil import disk_partitions
def diskinfo():
for i, disk in enumerate(disk_partitions()):
print "Disk #{0:d} {1:s}".format(i, disk.device)
print " Mount Point: {0:s}".format(disk.mountpoint)
print " File System: {0:s}".format(disk.fstype)
print " Options: {0:s}".format(disk.opts)
diskinfo()
Output:
bash-4.3# python /app/foo.py
Disk #0 /dev/mapper/docker-8:1-2762733-bdb0f27645efd726d69c77d0cd856d6218da5783b2879d9a83a797f8b896b4be
Mount Point: /
File System: ext4
Options: rw,relatime,discard,stripe=16,data=ordered
Upvotes: 1
Reputation: 388253
You could do something like this:
print("Disks:")
for disk in psutil.disk_partitions()):
print(disk)
That should look like this:
Disks:
sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed')
sdiskpart(device='D:\\', mountpoint='D:\\', fstype='', opts='cdrom')
sdiskpart(device='E:\\', mountpoint='E:\\', fstype='', opts='cdrom')
sdiskpart(device='F:\\', mountpoint='F:\\', fstype='NTFS', opts='rw,fixed')
sdiskpart(device='H:\\', mountpoint='H:\\', fstype='NTFS', opts='rw,removable')
Upvotes: 1