In Python, os.makedirs() with 0777 mode can not give others write permission
在Python中 ,使用0777模式的os.makedirs()无法给予其他人写入权限
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”
创建的目录没有权限“ 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.
未根据模式设置其他用户的写“ w”权限。
What’s wrong and how to fix it?
有什么问题以及如何解决?
Your permission bits may be turned off by umask. Try to make umask 000 first:
umask可能会关闭您的权限位。 尝试首先使umask 000 :
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.
它将首先清除umask以便提供的模式完全生效。
在Python中使用0777模式的os.makedirs()创建的目录无法给予其他人写入权限。问题在于权限位可能被umask关闭。解决方法是先调用os.umask(0)以确保提供的模式能完全生效。
`不会给予其他人写入权限&spm=1001.2101.3001.5002&articleId=107665408&d=1&t=3&u=120934c471de419b87d3b143fcb9f7a3)
441
`不会给予其他人写入权限&spm=1001.2101.3001.11663&articleId=107665408&d=1&t=3&u=aae052bb696f413fbebc2bd63af1a5ae)

被折叠的 条评论
为什么被折叠?



