Python 模块 os.chmod(file, 664) 不会改变 rw-rw-r-- 的权限,而是 -w--wx----

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15607903/
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-18 20:32:29  来源:igfitidea点击:

Python module os.chmod(file, 664) does not change the permission to rw-rw-r-- but -w--wx----

pythonfilepermissionschmod

提问by AplusG

Recently I am using Python module os, when I tried to change the permission of a file, I did not get the expected result. For example, I intended to change the permission to rw-rw-r--,

最近我在使用Python模块os,当我试图改变一个文件的权限时,我没有得到预期的结果。例如,我打算将权限更改为 rw-rw-r--,

os.chmod("/tmp/test_file", 664)

The ownership permission is actually -w--wx--- (230)

所有权权限实际上是-w--wx--- (230)

--w--wx--- 1 ag ag 0 Mar 25 05:45 test_file

However, if I change 664 to 0664 in the code, the result is just what I need, e.g.

但是,如果我在代码中将 664 更改为 0664,结果正是我所需要的,例如

os.chmod("/tmp/test_file", 0664)

The result is:

结果是:

-rw-rw-r-- 1 ag ag 0 Mar 25 05:55 test_file

Could anybody help explaining why does that leading 0 is so important to get the correct result?

有人可以帮助解释为什么前导 0 对获得正确结果如此重要吗?

采纳答案by RedBaron

Found this on a different forum

其他论坛上找到了这个

If you're wondering why that leading zero is important, it's because permissions are set as an octal integer, and Python automagically treats any integer with a leading zero as octal. So os.chmod("file", 484) (in decimal) would give the same result.

如果您想知道为什么前导零很重要,那是因为权限被设置为八进制整数,Python 会自动将任何带有前导零的整数视为八进制。所以 os.chmod("file", 484) (十进制)会给出相同的结果。

What you are doing is passing 664which in octal is 1230

你正在做的是传递664八进制是1230

In your case you would need

在你的情况下,你需要

os.chmod("/tmp/test_file", 436)


[Update] Note, for Python 3 you have prefix with 0o (zero oh). E.G, 0o666

[更新] 请注意,对于 Python 3,您的前缀为 0o(零哦)。例如,0o666

回答by lenik

leading 0means this is octalconstant, not the decimal one. and you need an octal to change file mode.

领先0意味着这是八进制常数,而不是十进制常数。并且您需要一个八进制来更改文件模式。

permissions are a bit mask, for example, rwxrwx---is 111111000in binary, and it's very easy to group bits by 3 to convert to the octal, than calculate the decimal representation.

权限是一个位掩码,例如,rwxrwx---111111000二进制的,很容易将位按 3 分组以转换为八进制,而不是计算十进制表示。

0644(octal) is 0.110.100.100in binary (i've added dots for readability), or, as you may calculate, 420in decimal.

0644(octal) 是0.110.100.100二进制的(为了可读性我已经添加了点),或者,你可以计算,420十进制。

回答by Dima

So for people who want semantics similar to:

因此,对于想要类似于以下语义的人:

$ chmod 755 somefile

Use:

用:

$ python -c "import os; os.chmod('somefile', 0o755)"

If your Python is older than 2.6:

如果您的 Python 版本早于 2.6:

$ python -c "import os; os.chmod('somefile', 0755)"

回答by mc.dev

If you have desired permissions saved to string then do

如果你有想要的权限保存到字符串然后做

s = '660'
os.chmod(file_path, int(s, base=8))

回答by Jason Drew

Using the stat.* bit masks does seem to me the most portable and explicit way of doing this. But on the other hand, I often forget how best to handle that. So, here's an example of masking out the 'group' and 'other' permissions and leaving 'owner' permissions untouched. Using bitmasks and subtraction is a useful pattern.

使用 stat.* 位掩码在我看来确实是最便携和最明确的方法。但另一方面,我经常忘记如何最好地处理它。因此,这是一个屏蔽“组”和“其他”权限并保持“所有者”权限不变的示例。使用位掩码和减法是一种有用的模式。

import os
import stat
def chmodme(pn):
    """Removes 'group' and 'other' perms. Doesn't touch 'owner' perms."""
    mode = os.stat(pn).st_mode
    mode -= (mode & (stat.S_IRWXG | stat.S_IRWXO))
    os.chmod(pn, mode)