In Python, `os.makedirs()` with 0777 mode does not give others write permission
Posted on In QAIn Python, os.makedirs()
with 0777 mode can not give others write permission
The code is as follows
$ python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.makedirs("/tmp/test1/test2", 0777)
>>>
The created dirs are not having permission “0777”
$ ls /tmp/test1/ -lrth
total 0
drwxrwxr-x 2 u1 u1 40 Oct 31 17:24 test2
The write ‘w’ permission for other users are not set according to the mode.
What’s wrong and how to fix it?
Your permission bits may be turned off by umask
. Try to make umask
000
first:
import os
# make dirs with mode
def mkdir_with_mode(directory, mode):
if not os.path.isdir(directory):
oldmask = os.umask(000)
os.makedirs(directory, 0777)
os.umask(oldmask)
It will clear the umask
first so that the mode provided takes full effect.