Python os.fchown()

Python os.fchown()

Python os模块中的所有函数在文件名和路径无效或不可访问,或其他具有正确类型但操作系统不接受的参数时都会引发OSError。

Python中的os.fchown()方法用于将与指定文件描述符相关联的文件的所有者和组id更改为指定的数字所有者id (UID)和组id (GID)。这个方法相当于os.chown(fd, uid, gid)方法。

注意:os.fchown()方法只在UNIX平台上可用,而且此方法的功能通常只对超级用户或特权用户可用。

语法:os.fchown(fd, uid, gid)

参数:

fd:一个文件描述符,表示要设置其uid和gid的文件

uid:一个整数值,表示要为文件设置的所有者id。

gid:一个整数值,表示要为文件设置的组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) 

输出:

Python os.fchown()

示例2

使用os.fchown()方法来设置任何一个id,并保持其他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) 

输出:

Python os.fchown()

示例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 ownership 
# 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 

输出:

Python os.fchown()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程