0

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 :)

1
  • 7
    pprint is your friend. Commented May 22, 2015 at 12:08

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.