gpt4 book ai didi

python - 为什么 python 中的 os.mkdir 设置权限的方式与 bash 中的 mkdir 之一不同?

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:59 28 4
gpt4 key购买 nike

我通过 bash 中的 mkdir 和 python 中的 os.mkdir 使用文件模式创建了目录。他们制作了具有不同权限的目录。

我在命令行中的测试代码如下,

$ mkdir -m 0775 aaa
$ cd aaa
$ mkdir -m 0777 bbb
$ python -c 'import os; os.mkdir("ccc",0o777)'

目录权限依次为aaa、bbb、ccc

directory aaa: drwxrwxr-x
directory bbb: drwxrwxrwx
directory ccc: drwxrwxr-x

bash 中的 mkdir 似乎不关心父目录的权限,但 python 中的 os.mkdir 关心。这样对吗? 为什么它们有不同的机制?

非常感谢。

最佳答案

mkdir(1)temporarily setting the umask如果您指定了一种模式,则为 0,如手册中的 secret 记录:

   -m, --mode=MODE
set file mode (as in chmod), not a=rwx - umask

Python 只是用给定的模式和通常的 umask 行为调用 mkdir(2) 系统调用。

mkdir(1) 的等效 Python 代码:

m = os.umask(0)
os.mkdir("ccc")
os.umask(m)

关于python - 为什么 python 中的 os.mkdir 设置权限的方式与 bash 中的 mkdir 之一不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46588363/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com