Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - explanation of pathlib.Path.mkdir mode argument

Tags:

python

pathlib

Please help understand how mode=511 is translated into the directory permission "775" in Pathlib.Path.mkdir().

  • Path.mkdir(mode=511, parents=False, exist_ok=False)

Path.mkdir(mode=511, parents=False, exist_ok=False) Create a new directory at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags.

import pathlib
pathlib.Path("/tmp/hoge").mkdir(mode=511)
$ ls -dal
drwxrwxr-x 2 user user 4096 Dec 30 16:24 /tmp/hoge

$ umask
0002

$ python --version
3.8.10

Not sure what is the logic behind.

# For mode=611
d--xr----t 2 user user 4096 Dec 30 16:37 /tmp/hoge

# For mode=411
drw--wx--x 2 user user 4096 Dec 30 16:38

# For mode=311
dr--rw-r-x 2 user user 4096 Dec 30 16:39 /tmp/hoge
like image 485
mon Avatar asked Oct 27 '25 10:10

mon


1 Answers

As per @Kris.

dec=511

print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")
---
The decimal value of 511 is:
0b111111111 in binary.
0o777 in octal. # <--- rwxrwxrwx
0x1ff in hexadecimal.

Then umask 002 is applied and becomes 775/rwxrwxrx.

like image 179
mon Avatar answered Oct 28 '25 22:10

mon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!