Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

mkdir not setting permissions correctly in python ?

By M.K. Verma · 3 May 2022

mkdir not setting permissions correctly in python ?

pathlib's mkdir essentially just calls os.mkdir(). The documentation there includes the section:

On some systems, mode is ignored. Where it is used, the current umask value is first masked out. If bits other than the last 9 (i.e. the last 3 digits of the octal representation of the mode) are set, their meaning is platform-dependent. On some platforms, they are ignored and you should call chmod() explicitly to set them.

So only the last 9 bits are guaranteed to be used. You'll have to do (no need to set umask):

md.mkdir()
md.chmod(0o2775)

It would be better if pathlib would not silently ignore extra bits, but actually threw a ValueError.