📜  Python| os.chown() 方法

📅  最后修改于: 2022-05-13 01:54:24.159000             🧑  作者: Mango

Python| os.chown() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。

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

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

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

代码 #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() 方法终端输出

代码 #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() 方法终端输出

代码#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 ownership 
# 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 ownership 
# 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() 方法终端输出

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