[Q] get device major/minor number

Dan M dan at catfolks.net
Tue Nov 30 21:18:38 CET 2010


On Tue, 30 Nov 2010 21:09:14 +0100, Thomas Portmann wrote:

> Hello all,
> 
> In a script I would like to extract all device infos from block or
> character device. The "stat" function gives me most of the infos (mode,
> timestamp, user and group id, ...), however I did not find how to get
> the devices major and minor numbers. Of course I could do it by calling
> an external program, but is it possible to stay within python?
> 
> In the example below, I would like to get the major (8) and minor (0, 1,
> 2) numbers of /dev/sda{,1,2}. How can I get them?

I think the os.major() and os.minor() calls ought to do what you want.

>>> import os
>>> s = os.stat('/dev/sda1')
>>> os.major(s.st_rdev)
8
>>> os.minor(s.st_rdev)
1
>>> 

dan at dan:~$ ls -l /dev/sda1
brw-rw---- 1 root disk 8, 1 2010-11-18 05:41 /dev/sda1




More information about the Python-list mailing list