[英]Using permissions for Linux chmod
I am trying the following way for executing /bin/chmod
. 我正在尝试以下方式来执行
/bin/chmod
。
from subprocess import Popen, PIPE
x = oct(0o755)
p = Popen(["/bin/chmod", x, "test.py"])
o,e = p.communicate()
When I am using the above script, I am getting the following output 当我使用上面的脚本时,我得到以下输出
[sujata@vm ~]$ /usr/local/bin/python3.6 test_chmod.py
/bin/chmod: invalid mode: ‘0o755’
Try '/bin/chmod --help' for more information.
In the above code if I am assigning x="0755"
and passing it to Popen
, it works as expected. 在上面的代码中,如果我分配
x="0755"
并将其传递给Popen
,它按预期工作。 However, Python3.6 has a different way of octal representation. 但是,Python3.6具有不同的八进制表示方式。 I am wondering if this is the right way or am I missing here something?
我想知道这是正确的方式还是我在这里错过了什么?
You can use os.chmod()
. 您可以使用
os.chmod()
。 The way to use it is: os.chmod($PATH$, stat.S_IWOTH)
the first parameter is the path to your file. 使用它的方法是:
os.chmod($PATH$, stat.S_IWOTH)
第一个参数是文件的路径。 Therefore you have to import os
. 因此你必须
import os
。 The seccond parameter is the mode. seccond参数是模式。 There are the following modes:
有以下模式:
You can look at https://www.tutorialspoint.com/python/os_chmod.htm 您可以查看https://www.tutorialspoint.com/python/os_chmod.htm
Solution 1: 解决方案1:
from subprocess import Popen, PIPE
x = '0755'
p = Popen(["/bin/chmod", x, "test.py"])
o,e = p.communicate()
Solution 2: 解决方案2:
import os
os.chmod("test.py", 0o755)
Solution 3: 解决方案3:
from pathlib import Path
Path("test.py").chmod(0o755)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.