Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。
如果文件名和路徑無效或無法訪問,或者具有正確類型但操作係統不接受的其他參數,則os模塊中的所有函數都會引發OSError。
os.fchown()
Python中的方法用於將與指定文件描述符關聯的文件的所有者和組ID更改為指定的數字所有者ID(UID)和組ID(GID)。此方法等效於os.chown(fd, uid, gid)
方法。
注意: os.fchown()
該方法僅在UNIX平台上可用,並且該方法的函數通常僅對超級用戶或特權用戶可用。
用法: os.fchown(fd, uid, gid)
參數:
fd:表示要設置其uid和gid的文件的文件描述符
uid:一個整數值,表示要為文件設置的所有者ID。
id:一個整數值,表示要為文件設置的組ID。要使任何一個ID保持不變,請將其設置為-1。
返回類型:此方法不返回任何值。
代碼1:os.fchown()方法的使用
# Python program to explain os.fchown() method
# importing os module
import os
# File
filename = "file.txt"
# Open the file and get the
# file descriptor associated
# with it using os.open() method
fd = os.open(filename, os.O_RDWR)
# Print the current owner id
# and group id of the file
# os.stat() method will return a
# 'stat_result’ object of
# ‘os.stat_result’ class whose
# 'st_uid' and 'st_gid' attributes
# will represent owner id and group id
# of the file respectively
print("Owner id of the file:", os.stat(fd).st_uid)
print("Group id of the file:", os.stat(fd).st_gid)
# Change the owner id and
# the group id of the file
# associated with the file descriptor
# using os.fchown() method
uid = 200
gid = 300
os.fchown(fd, uid, gid)
print("\nOwner and group id of the file changed")
# Print the owner id
# and group id of the file
print("\nOwner id of the file:", os.stat(fd).st_uid)
print("Group id of the file:", os.stat(fd).st_gid)
輸出:
代碼2:使用os.fchown()方法設置任何一個ID並保持其他不變
# Python program to explain os.fchown() method
# importing os module
import os
# File
filename = "file.txt"
# Open the file and get the
# file descriptor associated
# with it using os.open() method
fd = os.open(filename, os.O_RDWR)
# Print the current owner id
# and group id of the file
# os.stat() method will return a
# 'stat_result’ object of
# ‘os.stat_result’ class whose
# 'st_uid' and 'st_gid' attributes
# will represent owner id and group id
# of the file respectively
print("Owner id of the file:", os.stat(fd).st_uid)
print("Group id of the file:", os.stat(fd).st_gid)
# Change only group id of
# the file and leave owner id
# unchanged
# set id as -1 to leave
# it unchanged
uid = -1
gid = 1000
os.fchown(fd, uid, gid)
print("\ngroup id of the file changed")
# Print the owner id
# and group id of the file
print("\nOwner id of the file:", os.stat(fd).st_uid)
print("Group id of the file:", os.stat(fd).st_gid)
輸出:
代碼3:如果指定的路徑是符號鏈接
# Python program to explain os.fchown() method
# importing os module
import os
# File path
path = "./file.txt"
# Creating a symlink
# of the above path
# using os.symlink() method
symlink = "./file(symlink).txt"
os.symlink(path, symlink)
# Open the symlink
# and get the file descriptor
# associated with it using
# os.open() method
fd = os.open(symlink, os.O_RDWR)
# Print the current owner id
# and group id of the file
# as well as the symlink pointing
# to the above specified file path
print("Owner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
print("Owner id of the symlink:", os.stat(symlink).st_uid)
print("Group id of the symlink:", os.stat(symlink).st_gid)
# Change the owenership
# of the symlink pointing
# to the above file './file.txt'
uid = 600
gid = 700
os.fchown(fd, uid, gid)
print("\nOwner id and group id changed")
# Print the owner id
# and group id of the file
# as well as the symlink
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
print("Owner id of the symlink:", os.stat(symlink).st_uid)
print("Group id of the symlink:", os.stat(symlink).st_gid)
# As os.fchown() method
# follows symlink
# so, we can change the
# owner and group id
# through a symlink
輸出:
參考: https://docs.python.org/3/library/os.html
相關用法
- Python os.dup()用法及代碼示例
- Python next()用法及代碼示例
- Python set()用法及代碼示例
- Python hasattr()用法及代碼示例
- Python PIL putdata()用法及代碼示例
- Python PIL getcolors()用法及代碼示例
- Python PIL tobytes()用法及代碼示例
- Python PIL getpalette()用法及代碼示例
- Python os.get_blocking()用法及代碼示例
- Python Tensorflow cos()用法及代碼示例
- Python sympy.crt()用法及代碼示例
- Python sympy.nT()用法及代碼示例
- Python PIL putalpha()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.fchown() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。