gpt4 book ai didi

Python os.open() 无法将 umask 设置为 777(最大 755)

转载 作者:行者123 更新时间:2023-11-28 22:38:27 32 4
gpt4 key购买 nike

如果文件不存在,我的 python 脚本会创建一个文件,然后读取和写入该文件。该脚本可以由 root(自动)或由用户(刷新请求)运行。我需要创建具有写入权限的文件,以便在这两种情况下都可以重写文件。

import os
f = os.open('file', os.O_CREAT, 0777)
os.close(f)

但是……

$ ls -l
-rwxr-xr-x 1 pi pi 0 Feb 22 13:51 file

但是,这个脚本有效,我不明白其中的区别:

import os  
f = os.open('file', os.O_CREAT)
os.fchmod(f, 0777)
os.close(f)

...然后:

$ ls -l
-rwxrwxrwx 1 pi pi 0 Feb 22 13:54 file

最佳答案

您不是在设置 umask,您是在设置文件模式位,它们被 umask 屏蔽Per the documentation :

Open the file file and set various flags according to flags and possibly its mode according to mode. The default mode is 0777 (octal), and the current umask value is first masked out. ...

您的 umask 值似乎是 0022,因此屏蔽了组和其他用户的写入权限。

这个

os.fchmod(f, 0777)

尽管有 umask 值,但明确将文件权限设置为 0777

关于Python os.open() 无法将 umask 设置为 777(最大 755),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35564749/

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