简体   繁体   中英

python - explanation of pathlib.Path.mkdir mode argument

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) 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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM