I want to create a directory in Python using the same permissions as I’d get with the shell’s mkdir.
The standard Python documentation says:
os.mkdir(path[, mode])
Create a directory named path with numeric mode mode. The default mode
is 0777 (octal). On some systems, mode is ignored. Where it is used,
the current umask value is first masked out. If the directory already
exists, OSError is raised.
I don’t want the default 0777 permissions, but rather the permissions inherited from the parent directory. Can I do that without querying the parent directory’s permissions?
You already are getting the same permissions you’d get with the shell’s
mkdir.With the shell mkdir:
Or, more readably (from the BSD manpage):
Python’s
os.mkdirdoes the exact same thing:Python in fact calls the exact same POSIX mkdir function mentioned in the shell documentation with the exact same arguments. That function is defined as:
Or, more readably, from the FreeBSD/OS X manpage:
If you’re on a non-POSIX platform like Windows, Python tries to emulate POSIX behavior, even if the native shell has a command called
mkdirthat works differently. Mainly this is because the primary such shell is Windows, which has anmkdirthat’s a synonym for md, and the details of what it does as far as permissions aren’t even documented.