Python的lchmod()
方法将路径的模式更改为数字模式。如果path
是符号链接,则会影响符号链接而不是目标。 从Python 3.3开始,它相当于os.chmod(path,mode,follow_symlinks = False)
。
该模式可以采用以下值之一或其按位OR或它们的组合:
stat.S_ISUID
:执行时设置用户ID。stat.S_ISGID
:执行时设置组ID。stat.S_ENFMT
:强制执行记录锁定。stat.S_ISVTX
:执行后保存文本映像。stat.S_IREAD
:由所有者读取。stat.S_IWRITE
:由所有者写入。stat.S_IEXEC
:由所有者执行。stat.S_IRWXU
:由所有者读取,写入和执行。stat.S_IRUSR
:由所有者读取。stat.S_IWUSR
:由所有者写入。stat.S_IXUSR
:由所有者执行。stat.S_IRWXG
:按组读取,写入和执行。stat.S_IRGRP
:按组读取。stat.S_IWGRP
:按组写。stat.S_IXGRP
:按组执行。stat.S_IRWXO
:由其他人读取,写入和执行。stat.S_IROTH
:由其他人阅读stat.S_IWOTH
:由其他人写stat.S_IXOTH
:由其他人执行
注意 - 此方法已在Python 2.6中引入
语法
以下是lchmod()
方法的语法 -
os.lchmod(path, mode)
参数
- path - 这是要设置哪种模式的文件路径。
- flags - 这可能需要上述值之一或它们的按位OR组合。
返回值
- 此方法不返回任何值。
示例
以下示例显示了lchmod()
方法的用法。
#!/usr/bin/python3
import os, sys
# Open a file
path = "/var/www/html/foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )
# Close opened file
os.close( fd )
# Now change the file mode.
# Set a file execute by group.
os.lchmod( path, stat.S_IXGRP)
# Set a file write by others.
os.lchmod("/tmp/foo.txt", stat.S_IWOTH)
print ("Changed mode successfully!!")
执行上面代码后,将得到以下结果 -
Changed mode successfully!!
上一篇:
Python os模块方法
下一篇:
Python异常处理