Linux 如何在 Python 中使用 os.umask()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10291131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:57:36  来源:igfitidea点击:

How to use os.umask() in Python

pythonlinuxumask

提问by narnie

I'm trying to set a umask using the os module. Please note my normal umask set in my ~/.profile is umask 0027.

我正在尝试使用 os 模块设置 umask。请注意我在 ~/.profile 中设置的正常 umask 是 umask 0027。

In a bash shell,

在 bash shell 中,

umask 0022

will allow a file to be created with permissions

将允许创建具有权限的文件

-rw-r--r--

However, when us import the os module and do this:

但是,当我们导入 os 模块并执行以下操作时:

os.umask(0022)
[do some other code here that creates a file]

I get permissions of

我获得了以下权限

----------

First, how do I make os.umask(mask) behave like umask in the shell?

首先,如何让 os.umask(mask) 在 shell 中表现得像 umask?

Second, what is the logic between the difference of the two?

二、两者区别的逻辑是什么?

Note: I tried converting the 0022 to decimal in case it is expecting a decimal by doing:

注意:我尝试通过执行以下操作将 0022 转换为十进制,以防它需要十进制:

os.umask(18)

but it gave permissions of

但它给了权限

-----w--w-

Also note, I tried

另请注意,我试过

os.umask(00022)

and

os.mask(0o0022)

Which didn't work either.

这也不起作用。

回答by paxdiablo

You'll probably need to show us the code that constitutes:

您可能需要向我们展示构成以下内容的代码:

[do some other code here that creates a file]

The code you have works fine on my system:

您拥有的代码在我的系统上运行良好:

import os
oldmask = os.umask (022)
fh1 = os.open ("qq1.junk", os.O_CREAT, 0777)
fh2 = os.open ("qq2.junk", os.O_CREAT, 0022)
os.umask (oldmask)
os.close (fh1)
os.close (fh2)

producing files as follows:

生成文件如下:

-rwxr-xr-x 1 pax pax 0 Apr 24 11:11 qq1.junk
---------- 1 pax pax 0 Apr 24 11:11 qq2.junk

You should also note the restoration of the old umaskvalue which minimises the impact of changing it to the local operation.

您还应该注意旧umask值的恢复,这最大限度地减少了将其更改为本地操作的影响。

As you can see from the results above, you also need to be aware that the umaskvalue is "subtracted" from the mode you're using to create the file and we don't know what that mode is in your particular case.

正如您从上面的结果中看到的,您还需要注意该umask值是从您用于创建文件的模式中“减去”的,我们不知道在您的特定情况下该模式是什么。

That's evident even in your bashsample since a umaskvalue of 022when creating a file of mode 777would result in r-xr-xr-x, not rw-r--r--as you have it.

即使在您的bash示例中,这也很明显,因为创建 mode 文件时的umask值会导致,而不是您拥有的。022777r-xr-xr-xrw-r--r--



Based on your comments below where you indicate you're using openrather than os.open, a cursory glance of the Python source seems to indicate that this translates to a C fopencall which uses 0666as the initial mode. This is supported by the slightly modified code:

根据您在下面指出您使用的是open而不是 的评论os.open,粗略浏览 Python 源代码似乎表明这会转换为用作初始模式的 Cfopen调用0666。这是由稍微修改的代码支持的:

import os
oldmask = os.umask (022)
fh3 = open ("qq3.junk", "w")
os.umask (0)
fh4 = open ("qq4.junk", "w")
os.umask (oldmask)
fh3.close()
fh4.close()

which gives us:

这给了我们:

-rw-r--r-- 1 pax pax 0 Apr 24 11:44 qq3.junk
-rw-rw-rw- 1 pax pax 0 Apr 24 11:44 qq4.junk

So I'm not entirely certain why you're getting 0000permissions in your case.

所以我不完全确定为什么你会0000在你的情况下获得权限。

It would be worth seeing what the results are when you run that above program in your environment. If it's the same as I get then the problem may well lie somewhere else.

当您在您的环境中运行上述程序时,值得看看结果是什么。如果它和我得到的一样,那么问题很可能出在其他地方。

回答by George Lund

Being picky/careful, and Python 3k-compatible, here is my slightly different answer (that still doesn't explain what the OP's original issue was):

挑剔/小心,并且与 Python 3k 兼容,这是我略有不同的答案(这仍然不能解释 OP 的原始问题是什么):

old_umask = os.umask(0o022) # u=rwx,g=rx,o=rx
try:
    # do stuff

finally:
    os.umask(old_umask)

回答by JimH44

Misunderstanding of umask, I think. The umask sets the default denials, not the default permissions. So

我认为是对 umask 的误解。umask 设置默认拒绝,而不是默认权限。所以

import os
oldmask = os.umask (0o22)
fh1 = os.open ("qq1.junk", os.O_CREAT, 0o777)
fh2 = os.open ("qq2.junk", os.O_CREAT, 0o022)
os.umask (oldmask)
os.close (fh1)
os.close (fh2)

should indeed produce files as follows:

确实应该生成如下文件:

-rwxr-xr-x 1 pax pax 0 Apr 24 11:11 qq1.junk
---------- 1 pax pax 0 Apr 24 11:11 qq2.junk

The umask 022 removes write access for group and others, which is exactly the behaviour we see there. I find it helps to go back to the binary that the octal numbers represent:

umask 022 删除组和其他人的写访问权限,这正是我们在那里看到的行为。我发现回到八进制数代表的二进制是有帮助的:

 usr grp others 
-rwx rwx rwx is represented in octal as 0777, requested for qq1.junk
-000 010 010 umask of 022 removes any permission where there is a 1
-rwx r-x r-x is the result achieved requesting 0777 with umask of 022

---- -w- -w- is represented in octal as 0022, requested for qq2.junk
-000 010 010 umask of 022 removes any permission where there is a 1
---- --- --- is the result achieved requesting 0022 with umask of 022

The program is behaving as you asked it to, not necessarily as you thought it should. Common situation, that, with computers :-)

程序按照您的要求运行,不一定按照您的预期运行。常见的情况,即,与计算机:-)

回答by fatal_error

Even though this would seem to be a straight system call, in this case it does seems to matter what Python version you are using:

尽管这似乎是一个直接的系统调用,但在这种情况下,您使用的 Python 版本似乎很重要:

It appears that os.open handles the pre-existing umask differently in Python 2.x and Python 3.x, possibly because 2.x is closer to the OS and 3.x does a bit more abstraction.

似乎 os.open 在 Python 2.x 和 Python 3.x 中处理预先存在的 umask 的方式不同,可能是因为 2.x 更接近操作系统,而 3.x 做了更多抽象。

https://docs.python.org/2/library/os.html"The default mode is 0777 (octal), and the current umask value is first masked out."

https://docs.python.org/2/library/os.html"默认模式为0777(八进制),先屏蔽掉当前的umask值。"

There is no similar statement in https://docs.python.org/3/library/os.html

https://docs.python.org/3/library/os.html 中没有类似的声明