在Python中,使用0777模式的`os.makedirs()`不会给予其他人写入权限

在Python中使用0777模式的os.makedirs()创建的目录无法给予其他人写入权限。问题在于权限位可能被umask关闭。解决方法是先调用os.umask(0)以确保提供的模式能完全生效。
Python3.8

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

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以便提供的模式完全生效。

Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/in-python-os-makedirs-with-0777-mode-does-not-give-others-write-permission/

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值