Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。
在Unix-like系统中,“模式”是授予用户,组和其他类以访问文件的文件系统权限。
os.fchmod()
Python中的方法用于将指定文件描述符给定的文件模式更改为指定数字模式。此方法等效于os.chmod(fd, mode)
。
注意:此方法仅在Unix平台上可用。
用法: os.fchmod(fd, mode)
参数:
fd:要设置其模式的文件描述符。
mode:表示要设置的模式的数值。
模式也可以采用以下值之一或它们的按位或组合:
- 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:被他人执行
返回类型:此方法不返回任何值。
代码:使用os.fchmod()方法
# Python program to explain os.fchmod() method
# importing os module
import os
# importing stat module
import stat
# File name
filename = "file.txt"
# Open the specified file and
# get the file descriptor
# associated with it using
# os.open() method
fd = os.open(filename, os.O_RDWR)
# Print the current numeric mode
# (octal notation ) of the file
mode = oct(os.stat(fd).st_mode)[-3:]
print("Current numeric mode of the file (octal notation):", mode)
# Now change the mode
# of the file
# octal value 777 as mode means
# read write and execute permission
# for owner, group and others
mode = 0o777
os.fchmod(fd, mode)
print("\nFile mode changed successfully")
# Print the changed numeric mode
# (octal notation ) of the file
mode = oct(os.stat(fd).st_mode)[-3:]
print("Current numeric mode of the file (octal notation):", mode)
# mode parameter can be also
# given by flags defined in
# stat module
# Chnage mode
mode = stat.S_IRWXU
os.fchmod(fd, mode)
print("\nFile mode changed successfully")
print("Now, File can be read, write and executed by owner only")
# Print the changed numeric mode
# (octal notation ) of the file
mode = oct(os.stat(fd).st_mode)[-3:]
print("Current numeric mode of the file (octal notation):", mode)
# change mode
mode = stat.S_IRWXU | stat.S_IRGRP
os.fchmod(fd, mode)
print("\nFile mode changed successfully")
print("Now, File can be read, write and executed \
by owner but can be read by group")
# Print the changed numeric mode
# (octal notation ) of the file
mode = oct(os.stat(fd).st_mode)[-3:]
print("Current numeric mode of the file (octal notation):", mode)
# Close the file descriptor
os.close(fd)
输出:
Current numeric mode of the file (octal notation): 666 File mode changed successfully Current numeric mode of the file (octal notation): 777 File mode changed successfully Now, File can be read, write and executed by owner only Current numeric mode of the file (octal notation): 700 File mode changed successfully Now, File can be read, write and executed by owner but can be read by group Current numeric mode of the file (octal notation): 740
相关用法
- Python set()用法及代码示例
- Python next()用法及代码示例
- Python os.dup()用法及代码示例
- Python PyTorch sin()用法及代码示例
- Python os.confstr()用法及代码示例
- Python os.write()用法及代码示例
- Python os.read()用法及代码示例
- Python os.statvfs()用法及代码示例
- Python sympy.div()用法及代码示例
- Python os.strerror()用法及代码示例
- Python os.fstatvfs()用法及代码示例
- Python os.sysconf()用法及代码示例
- Python sympy.cot()用法及代码示例
注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.fchmod() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。