mora
mora

Reputation: 2287

Why is the way of setting permission by os.mkdir in python different from one of mkdir in bash?

I made directories with file mode by mkdir in bash and os.mkdir in python. They made directories with different permissions.

My test code in command line is following,

$ mkdir -m 0775 aaa
$ cd aaa
$ mkdir -m 0777 bbb
$ python -c 'import os; os.mkdir("ccc",0o777)'

The permission of directories, aaa, bbb and ccc are following

directory aaa: drwxrwxr-x
directory bbb: drwxrwxrwx
directory ccc: drwxrwxr-x

It seems that mkdir in bash does not care the permission of parent directory but os.mkdir in python does. Is it right? And why do they have different mechanism?

Thank you very much.

Upvotes: 2

Views: 649

Answers (1)

Josh Lee
Josh Lee

Reputation: 177855

mkdir(1) is temporarily setting the umask to 0 if you specify a mode, as cryptically documented in the manual:

   -m, --mode=MODE
          set file mode (as in chmod), not a=rwx - umask

Python is just calling the mkdir(2) syscall with the mode given and the usual umask behavior.

The equivalent Python code to what mkdir(1) is doing:

m = os.umask(0)
os.mkdir("ccc")
os.umask(m)

Upvotes: 2

Related Questions