10

Can anyone tell me what is the meaning of the number from the ST_MODE function?

Example:

>>>import os
>>>stat = os.stat('/home')  
>>>print stat.st_mode  
16877  

it prints 16877. What is that for?

youri
  • 3,685
  • 5
  • 23
  • 43
Egy Mohammad Erdin
  • 3,402
  • 6
  • 33
  • 57

2 Answers2

31

It's the permission bits of the file.

>>> oct(16877)
'040755'

See the various stat.S_* attributes for more info.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 3
    AFAIK in addition to permissions, st_mode is for testing whether a file is of a specified type (FIFO, link, etc) – Eli Bendersky Nov 03 '10 at 13:32
  • 2
    For the lazy: the most significant (leftmost) two octal digits specify file type, the four least significant are of course the normal file permissions bits. Get these permissions bits with stat.S_IMODE. – ACK_stoverflow Jul 18 '19 at 19:05
  • @ACK_stoverflow shouldn't permissions be represented by 3 numbers (owner, group, others) and not 4? – robertspierre Jul 27 '22 at 07:40
  • @robertspierre People usually drop the leading (fourth) octal digit because it's for special things that you normally don't care about - suid, sgid, and sticky bit, and omitting it just means it's zero. From `man chmod`: `The first digit selects the set user ID (4) and set group ID (2) and restricted deletion or sticky (1) attributes.` – ACK_stoverflow Jul 27 '22 at 21:40
  • @robertspierre Use "stat.S_IMODE(mode)" to get the 3 numbers that chmod understands from the mode Python outputs. – William Stein Oct 25 '22 at 20:12
1

The standard stat module can help you interpret these values from os.stat:

The stat module defines constants and functions for interpreting the results of os.stat(), os.fstat() and os.lstat() (if they exist). For complete details about the stat(), fstat() and lstat() calls, consult the documentation for your system.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412