Friday, December 23, 2016

Using psutil to get disk partition information with Python

By Vasudev Ram

psutil is a Python library that enables you to get various kinds of operating system information, such as about CPUs, processes, memory and disks.

Here is a short program that shows how to get information about the disk partitions on your computer using psutil:
from __future__ import print_function
import psutil

dps = psutil.disk_partitions()
fmt_str = "{:<8} {:<7} {:<7}"
print(fmt_str.format("Drive", "Type", "Opts"))
# Only show a couple of devices.
for i in (0, 2):
    dp = dps[i]
    print(fmt_str.format(dp.device, dp.fstype, dp.opts))
Running the program gives this output:
$ py -3 psutil_disk_partitions.py
Drive    Type    Opts
C:\      NTFS    rw,fixed
E:\      CDFS    ro,cdrom

(Check out py, the Python launcher, if you don't already use it.)

Explanation of the above output:
The Drive column shows the drive letter.
The Type column shows the file system type of the partition.
The Opts column shows what options were used while mounting the device on the drive.

Mounting is the operation of logically associating a drive name or a path name with a file system (on a partition or physical drive), and making it accessible under that drive or path name.

For this run, it shows that:
C drive is an NTFS partition (Windows NT File System) and is mounted in read-write mode and is a fixed disk;
E drive is a CDFS partition (CD-ROM File System) and is mounted in read-only mode.

Here is a video about how a hard disk drive works:



- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Managed WordPress Hosting by FlyWheel



No comments: