2

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.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
mora
  • 2,217
  • 4
  • 22
  • 32
  • i don't know the answer off the bat but i'm guessing python uses its own umask... something to look into and (dis)confirm – amphibient Oct 05 '17 at 14:45

1 Answers1

1

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)
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • thank you for answering it. I will use bash way even in python code by your solution. thank you again. – mora Oct 05 '17 at 15:04