当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python os.chown()用法及代码示例


Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。

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

os.chown()Python中的方法用于将指定路径的所有者和组ID更改为指定的数字所有者ID(UID)和组ID(GID)。


注意: os.chown()该方法仅在UNIX平台上可用,并且该方法的函数通常仅对超级用户或特权用户可用。

用法: os.chown(path, uid, gid, *, dir_fd = None, follow_symlinks = True) 

参数:
路径:表示要设置其uid和gid的文件的文件描述符
uid:一个整数值,表示要为路径设置的所有者ID。
id:一个整数值,表示要为路径设置的组ID。要使任何一个ID保持不变,请将其设置为-1。
dir_fd(可选):引用目录的文件描述符。此参数的默认值为“无”。
follow_symlinks(可选):此参数的默认值为True。如果我们不希望os.chown()方法遵循符号链接,则可以将其设置为False。如果为False,则方法将对符号链接本身进行操作,而不是对链接指向的文件进行操作。

注意:参数列表中的“ *”表示以下所有参数(在本例中为“ dir_fd”和“ follow_symlinks”)均为keyword-only参数,可以使用其名称(而不是位置参数)来提供。

返回类型:此方法不返回任何值。

代码1:os.chown()方法的使用
# Python program to explain os.chown() method  
    
# importing os module  
import os 
  
# File path 
path = "./file.txt"
  
# Print the current owner id 
# and group id of the 
# specified file path 
  
# 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(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid)  
  
  
# Change the owner id and  
# the group id of the file 
# using os.chown() method 
uid = 2000
gid = 2000
os.chown(path, 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(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid) 

输出:
os.chown() method terminal output

代码2:使用os.chown()方法设置任何一个ID并保持其他不变
# Python program to explain os.chown() method  
    
# importing os module  
import os 
  
# File 
path = "./file.txt"
  
# 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(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid)  
  
  
# Change only owner id of  
# the file and leave 
# group id unchanged 
  
# set id as -1 to leave 
# it unchanged 
uid = 3000
gid = -1
os.chown(path, uid, gid) 
print("\nOwner id of the file changed") 
  
  
# Print the owner id 
# and group id of the file 
print("\nOwner id of the file:", os.stat(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid) 

输出:
os.chown() method terminal output

代码3:如果指定的路径是符号链接
# Python program to explain os.chown() 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) 
  
  
# 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 = 1000
gid = 1000
os.chown(symlink, 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.chown() method 
# follows symlink 
# so, we can change the 
# owner and group id  
# through a symlink  
  
  
# We can modify the follow_symlinks 
# parameter if we do not 
# want os.chown() method to 
# follow symlink 
  
  
# Change the owenership  
# of the symlink pointing  
# to the above file './file.txt' 
uid = 4000
gid = 4000
os.chown(symlink, uid, gid, follow_symlinks = False) 
print("\nOwner id and group id not changed") 
  
  
# Print the owner id 
# and group id of the file 
# as well as the symlink 
# pointing to it 
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)

输出:
os.chown() method terminal output

参考: https://docs.python.org/3/library/os.html



相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.chown() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。