Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Vasudev Ram: Using psutil to get disk partition information with Python

$
0
0

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 on my forthcoming apps and content.Jump to posts:**Follow me on:*


Viewing all articles
Browse latest Browse all 9596

Trending Articles