Python 模块 os.chmod(file, 664) 不会改变 rw-rw-r-- 的权限,而是 -w--wx---- [英] Python module os.chmod(file, 664) does not change the permission to rw-rw-r-- but -w--wx----

查看:40
本文介绍了Python 模块 os.chmod(file, 664) 不会改变 rw-rw-r-- 的权限,而是 -w--wx----的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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

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

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

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)

结果是:

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

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

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

推荐答案

不同论坛

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

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.

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

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

在你的情况下,你需要

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

<小时>

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

这篇关于Python 模块 os.chmod(file, 664) 不会改变 rw-rw-r-- 的权限,而是 -w--wx----的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆