问题:Python模块os.chmod(file,664)不会将权限更改为rw-rw-r,而是-w–wx —-

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

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

所有权许可实际上是-w–wx —(230)

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

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

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

结果是:

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

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

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–,

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

The ownership permission is actually -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.

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

其他论坛上找到了这个

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

您正在做的是通过664八进制的1230

在您的情况下,您将需要

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

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

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.

What you are doing is passing 664 which in octal is 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


回答 1

因此,对于那些想要类似语义的人:

$ chmod 755 somefile

用:

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

如果您的Python早于2.6:

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

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 -c "import os; os.chmod('somefile', 0755)"

回答 2

前导0意味着这是八进制常数,而不是十进制数。并且您需要八进制更改文件模式。

权限是一个位掩码,例如,rwxrwx---111111000二进制的,并且将位除以3来转换为八进制,这比计算十进制表示形式要容易得多。

0644(八进制)为0.110.100.100二进制(为了可读性,添加了点),或者,如您所计算的,420十进制。

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

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

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


回答 3

使用权限符号代替数字

如果您使用了语义上更命名的权限符号而不是原始的魔术数字,则可以避免您的问题,例如664

#!/usr/bin/env python3

import os
import stat

os.chmod(
    'myfile',
    stat.S_IRUSR |
    stat.S_IWUSR |
    stat.S_IRGRP |
    stat.S_IWGRP |
    stat.S_IROTH
)

https://docs.python.org/3/library/os.html#os.chmod中对此进行了记录,其名称与在处记录的POSIX C API值相同man 2 stat

另一个优点是文档中提到的更大的可移植性:

注意:尽管Windows支持chmod(),但您只能使用它设置文件的只读标志(通过stat.S_IWRITEstat.S_IREAD常数或相应的整数值)。所有其他位均被忽略。

chmod +x演示于:如何在python中执行简单的“ chmod + x”?

已在Ubuntu 16.04,Python 3.5.2中进行了测试。

Use permission symbols instead of numbers

Your problem would have been avoided if you had used the more semantically named permission symbols rather than raw magic numbers, e.g. for 664:

#!/usr/bin/env python3

import os
import stat

os.chmod(
    'myfile',
    stat.S_IRUSR |
    stat.S_IWUSR |
    stat.S_IRGRP |
    stat.S_IWGRP |
    stat.S_IROTH
)

This is documented at https://docs.python.org/3/library/os.html#os.chmod and the names are the same as the POSIX C API values documented at man 2 stat.

Another advantage is the greater portability as mentioned in the docs:

Note: Although Windows supports chmod(), you can only set the file’s read-only flag with it (via the stat.S_IWRITE and stat.S_IREAD constants or a corresponding integer value). All other bits are ignored.

chmod +x is demonstrated at: How do you do a simple “chmod +x” from within python?

Tested in Ubuntu 16.04, Python 3.5.2.


回答 4

如果您已将所需的权限保存到字符串,请执行

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

If you have desired permissions saved to string then do

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

回答 5

在我看来,使用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)

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.

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)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。