How to get the minor and major device numbers with os.stat ?

Erik Max Francis max at alcyone.com
Mon Aug 25 00:35:59 CEST 2003


Alain Tesio wrote:

> I expected major*256+minor, I've tried a lot of combinations at random
> and couldn't manage to get 17383 from (13,7)

For the answer to this, you should really be looking at man stat.  The
values here are implementation defined; Python's just mimicking whatever
the underlying OS is doing.  What you want is st_rdev:

>>> import os
>>> os.system('ls -l /dev/ttyS0')
crw-rw----    1 root     uucp       4,  64 Jul 17  1994 /dev/ttyS0
0
>>> s = os.stat('/dev/ttyS0')
>>> tuple(s)
(8624, 9064L, 769L, 1, 0, 14, 0L, 1058628788, 774488935, 1058628789)
>>> s.st_rdev
1088
>>> divmod(1088, 256)
(4, 64)

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Get married, but never to a man who is home all day.
\__/  George Bernard Shaw




More information about the Python-list mailing list