I know that a am little bit curious.Because i believe if i go deep , i can get it faster and in a short time i can be expert..Today Some things caught my eye in "stat" module in python 3.7

Sometimes we need status of file or directory.We can see it with like that.

>>>import pathlib as ph
>>>x=ph.Path('D://Music')
WindowsPath('D:/Music')
x.is_dir()
True

There is no problem.Then i go to inside of pathlib for whats going on inital state.And I knew that i would encounter stat module in it.

if we do this:

x.stat()
#Then we will get a tuple like this
os.stat_result(st_mode=16749, st_ino=28147497........

So stat module to find out if the given address is a directory or a file takes "st_mode" , in this case this number 16749 And magic begins here.

Following codes are from inside of stat module.

Our number (16749) first crossing from S_IFMT , so will be &-ed with value.

def S_IMODE(mode):
    """Return the portion of the file's mode that can be set by
    os.chmod().
    """
    return mode & 0o7777

def S_IFMT(mode):
    """Return the portion of the file's mode that describes the
    file type.
    """
    return mode & 0o170000

# Constants used as S_IFMT() for various file types
# (not all are implemented on all systems)

S_IFDIR  = 0o040000  # directory

if we do it manually :

>>> 16749&0o170000
16384
>>> oct(16384)
'0o40000'     #really is that   S_IFDIR  = 0o040000  # directory

It's looking that this value comes from built'in os level ,cause stat tuple of x file returned it (st_mode).So question is this.

1.Where comes these values? like S_IFDIR there a few variabls keeps this "Octal" values like this 0o040000 .

2.Why this octal number & ed , why not or , xor?

Thank you in advance for your help.

0 Answers