简体   繁体   English

使用Linux chmod的权限

[英]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: 有以下模式:

  • stat.S_ISUID − Set user ID on execution. stat.S_ISUID - 在执行时设置用户ID。
  • stat.S_ISGID − Set group ID on execution. stat.S_ISGID - 执行时设置组ID。
  • stat.S_ENFMT − Record locking enforced. stat.S_ENFMT - 强制执行记录锁定。
  • stat.S_ISVTX − Save text image after execution. stat.S_ISVTX - 执行后保存文本图像。
  • stat.S_IREAD − Read by owner. stat.S_IREAD - 由所有者阅读。
  • stat.S_IWRITE − Write by owner. stat.S_IWRITE - 由所有者写。
  • stat.S_IEXEC − Execute by owner. stat.S_IEXEC - 由所有者执行。
  • stat.S_IRWXU − Read, write, and execute by owner. stat.S_IRWXU - 由所有者读取,写入和执行。
  • stat.S_IRUSR − Read by owner. stat.S_IRUSR - 由所有者阅读。
  • stat.S_IWUSR − Write by owner. stat.S_IWUSR - 由所有者写。
  • stat.S_IXUSR − Execute by owner. stat.S_IXUSR - 由所有者执行。
  • stat.S_IRWXG − Read, write, and execute by group. stat.S_IRWXG - 按组读取,写入和执行。
  • stat.S_IRGRP − Read by group. stat.S_IRGRP - 按组读取。
  • stat.S_IWGRP − Write by group. stat.S_IWGRP - 按组写。
  • stat.S_IXGRP − Execute by group. stat.S_IXGRP - 按组执行。
  • stat.S_IRWXO − Read, write, and execute by others. stat.S_IRWXO - 由他人读取,写入和执行。
  • stat.S_IROTH − Read by others. stat.S_IROTH - 由其他人阅读。
  • stat.S_IWOTH − Write by others. stat.S_IWOTH - 由其他人撰写。
  • stat.S_IXOTH − Execute by others. stat.S_IXOTH - 由他人执行。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM