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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。