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

Discussion in 'Python' started by Alain Tesio, Aug 24, 2003.

  1. Alain Tesio

    Alain Tesio Guest

    Hi, I can't manage to get the major or minor device numbers
    with os.stat :

    ~ $ll /dev/xda7 /dev/xda8
    brw-rw---- 1 root disk 13, 7 Nov 30 2000 /dev/xda7
    brw-rw---- 1 root disk 13, 8 Nov 30 2000 /dev/xda8

    ~ $python
    Python 2.2.2 (#4, Oct 15 2002, 04:21:28)
    [GCC 2.95.4 20011002 (Debian prerelease)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.stat("/dev/xda7")

    (25008, 17383L, 5643L, 1, 0, 6, 0L, 1007270448, 975597764, 1007270448)
    >>> os.stat("/dev/xda8")

    (25008, 17384L, 5643L, 1, 0, 6, 0L, 1007270448, 975597764, 1007270448)
    >>> 17384 / 256 , 17384 % 256

    (67, 232)


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

    Any idea ?

    Thanks
    Alain
    Alain Tesio, Aug 24, 2003
    #1
    1. Advertising

  2. 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 && && 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
    Erik Max Francis, Aug 24, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dGhlV2l6YXJkMQ==?=

    Publish and specify Major, Minor Version numbers using asp.net 2.0

    =?Utf-8?B?dGhlV2l6YXJkMQ==?=, Apr 26, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,662
    Juan T. Llibre
    Apr 26, 2006
  2. Steven T. Hatton
    Replies:
    3
    Views:
    9,692
    Pete Becker
    Jun 2, 2005
  3. Jef Driesen
    Replies:
    2
    Views:
    1,672
    Jef Driesen
    Jan 12, 2006
  4. Thomas Portmann

    [Q] get device major/minor number

    Thomas Portmann, Nov 30, 2010, in forum: Python
    Replies:
    1
    Views:
    728
    Thomas Portmann
    Nov 30, 2010
  5. Brent S. Elmer Ph.D.
    Replies:
    0
    Views:
    51
    Brent S. Elmer Ph.D.
    Apr 29, 2014
Loading...

Share This Page