Python os.chown()

Python os.chown()

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

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

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

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

参数:

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

uid:整数值,表示要为路径设置的所有者id。

gid:整型值,表示要为该路径设置的组id。要保持任何一个id不变,请将其设置为-1。

dir_fd(可选):指向目录的文件描述符。这个参数的默认值是None。

follow_symlinks(可选):默认值为True。如果不希望os.chown()方法遵循符号链接,可以将其设置为False。如果为False,则方法将操作符号链接本身,而不是链接指向的文件。

注意:参数列表中的’ * ‘表示以下所有参数(在我们的例子中是’ dir_fd ‘和’ follow_symlinks ‘)都是仅关键字参数,可以使用它们的名称提供它们,而不是作为位置参数。

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

示例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) 

输出:

Python os.chown()

示例2

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

输出:

Python 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)

输出:

Python os.chown()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程