Python的os.fchmod()
方法将fd
给出的文件的模式更改为数字模式。该模式可以采用以下值之一或按位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以上版本。
语法
以下是fchmod()
方法的语法 -
os.fchmod(fd, mode)
参数
- fd − 这是将设置哪种模式的文件描述符。
- mode − 这可能需要上述值之一或按位OR或它们的组合。
返回值
- 此方法不返回任何值。仅适用于Unix操作系统。
示例
以下示例显示了fchmod()
方法的用法。
#!/usr/bin/python3
import os, sys, stat
# Now open a file "/tmp/foo.txt"
fd = os.open( "/tmp", os.O_RDONLY )
# Set a file execute by the group.
os.fchmod( fd, stat.S_IXGRP)
# Set a file write by others.
os.fchmod(fd, stat.S_IWOTH)
print ("Changed mode successfully!!")
# Close opened file.
os.close( fd )
执行上面代码后,将得到以下结果 -
Changed mode successfully!!
上一篇:
Python os模块方法
下一篇:
Python异常处理