[Q] get device major/minor number

T

Thomas Portmann

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?

user@host:~$ ls -l /dev/sda /dev/sda1 /dev/sda2
brw-rw---- 1 root disk 8, 0 Nov 30 19:10 /dev/sda
brw-rw---- 1 root disk 8, 1 Nov 30 19:10 /dev/sda1
brw-rw---- 1 root disk 8, 2 Nov 30 19:10 /dev/sda2
user@host:~$ python3.1 -c 'import os
for el in ["","1","2"]: print(os.stat("/dev/sda"+el));'
posix.stat_result(st_mode=25008, st_ino=1776, st_dev=5, st_nlink=1,
st_uid=0, st_gid=6, st_size=0, st_atime=1291140641,
st_mtime=1291140640, st_ctime=1291140640)
posix.stat_result(st_mode=25008, st_ino=1780, st_dev=5, st_nlink=1,
st_uid=0, st_gid=6, st_size=0, st_atime=1291140644,
st_mtime=1291140641, st_ctime=1291140641)
posix.stat_result(st_mode=25008, st_ino=1781, st_dev=5, st_nlink=1,
st_uid=0, st_gid=6, st_size=0, st_atime=1291140644,
st_mtime=1291140641, st_ctime=1291140641)

Thanks


Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top