Showing posts with label PsUtil. Show all posts
Showing posts with label PsUtil. Show all posts

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



Thursday, November 1, 2012

Glances CLI monitoring tool uses psutil


By Vasudev Ram

Interesting to see that the Glances system monitoring tool uses psutil. IIRC, I had blogged or tweeted about Glances some time ago.

- Vasudev Ram - Dancing Bison Enterprises



Wednesday, October 31, 2012

psutil, Python tool to get process info and more


By Vasudev Ram

psutil is a versatile and cross-platform Python module for getting process and related information from the operating system.

Though the name psutil indicates that it works like the UNIX ps command (for process status), it does a lot more, and works on other platforms too.

IIRC, I had come across it earlier, but thought it was only for UNIX-like systems. I recently tried out psutil on Windows.

For certain Windows system processes, it may not give some info, due to those processes being privileged, according to one of the psutil authors, Giampaolo Rodola, who I emailed about it. He said: "On Windows you can't access all information about processes owned by NT AUTHORITY SYSTEM user. In those cases psutil will raise AccessDenied exception."

psutil can provide some information on:

- CPU time
- virtual memory
- swap memory (sic)
- disk partitions
- disk usage
- disk and network I/O
- users
- processes: name, executable name, working directory, command line, user name, user ids and group ids, threads, connections, file descriptors.

Here is a simple psutil test program which shows just a few features - output not well formatted, but readable. Try it on Windows or UNIX/Linux:

# test_psutil_00.py

import psutil

for pid in psutil.get_pid_list():
        p = psutil.Process(pid)
        p_name = p.name
        print "p_name:", p.name
        try:
                p_exe = p.exe
        except Exception, e:
                p_exe = "Can't access p.exe"
        print "p_exe:", p_exe
        try:
                p_cwd = p.getcwd()
        except Exception, e:
                p_cwd = "Can't access p.cwd()"
        print "p.getcwd():", p_cwd
        try:
                p_cmdline = p.cmdline
        except Exception, e:
                p_cmdline = "Can't access p.cmdline"
        print "p.cmdline:", p_cmdline

- Vasudev Ram

Tuesday, September 4, 2012

Glances, CLI/curses Python tool to monitor UNIX systems

By Vasudev Ram


Glances is "a CLI curses based monitoring tool for GNU/Linux and BSD OS". It uses Python and PsUtil.

Hacker News thread about it.

The thread has some positive comments about Glances.

EDIT: Sorry, readers, about the temporary multiple posts on the same topic. Due to getting some Blogger error, I clicked Submit a few times, so multiple posts resulted. I've deleted the duplicate posts now. The duplicate posts only existed for a few minutes before I deleted them, but mentioning it because I don't anyone to think I'm spamming them.

- Vasudev Ram - Dancing Bison Enterprises