Python os.chown() 方法



Python os.chown() 方法将 path 的 owner 和 group id 修改为指定的 uid 和 gid。要保持其中一个 ID 不变,请将 uid 或 gid 设置为 -1。要执行修改操作,您需要超级用户权限。

为了确定用户或组可以访问哪些系统资源,类 Unix 操作系统通过称为用户标识符 (UID) 的值来识别用户,并通过组标识符 (GID) 来识别组。

注意:此方法仅在 UNIX/LINUX 平台中可用。

语法

以下是 Python os.chown() 方法的语法 -


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

参数

  • path − 这是需要设置所有者 ID 和组 ID 的路径。
  • uid - 这是要为文件设置的 Owner ID。
  • gid − 这是要为文件设置的组 ID。
  • dir_fd − 指向目录的文件描述符。默认情况下,它是 None。
  • follow_symlinks − 默认情况下为 True。当 False 时,该方法将对符号链接本身进行操作,而不是对链接指向的文件进行操作。

注意:参数列表中的 * 表示所有后续参数都是关键字参数(在本例中为 dir_fd 和 follow_symlinks),即非位置参数。

返回值

此方法不返回任何值。

示例 1

如果我们不想更改 gid,我们可以将其设置为 -1。

以下示例显示了 Python os.chown() 方法的用法。此处,文件路径的所有权更改为给定的 uid '100' 和 gid '-1'。因此,仅修改 uid。


import os, sys
# Assuming /tmp/foo.txt exists.
# To set owner ID 100 following has to be done.
os.chown("/tmp/foo.txt", 100, -1)
print ("Changed ownership successfully!!")

当我们运行上述程序时,它会产生以下结果——

Changed ownership successfully!!

示例 2

在这里,我们尝试更改文件路径的 uid 和 gid。给定的 uid 为 '3000',gid 为 '2000'。


# importing the module
import os
filepath = "code.txt"
# Printing the current UID and GID of the file
print("The current UID of the file is:", os.stat(filepath).st_uid)
print("The current GID of the file is:", os.stat(filepath).st_gid)
# Using the method
os.chown(path, 3000, 2000)
print("\n The UID and GID of this file has been changed succesfully")
# Printing the UID and GID of the file after modifying
print("\n The modified uid of the file is:", os.stat(filepath).st_uid)
print("The modified gid of the file is:", os.stat(filepath).st_gid)

在执行上述代码时,我们得到以下输出 -

sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py
The current UID of the file is: 1000
The current GID of the file is: 1000

The UID and GID of this file has been changed succesfully

The modified uid of the file is: 3000
The modified gid of the file is: 2000

示例 3

默认情况下,follow_symlink为 True。当 False 时,该方法将对符号链接本身进行操作,而不是对链接指向的文件进行操作。

在下面给出的示例中,创建了给定文件路径“code.txt”的符号链接。然后我们尝试更改指向文件路径的符号链接的 uid 和 gid。此处使用默认follow_symlink。


import os
filepath = "code.txt"
# creating a symlink for filepath
symlink_path = "code_symlink.txt"
os.symlink(filepath, symlink_path)

# Printing the UID and GID of the filepath and symlink_path
print("The UID of the filepath is:", os.stat(filepath).st_uid)
print("The GID of the filepath is:", os.stat(filepath).st_gid)
print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid)
print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)

# changing the symlink_path UID and GID of the file with default follow_symlinks
os.chown(symlink_path, 5000, 5000)
# Printing the UID and GID of the filepath and symlink_path
print("changing the symlink_path UID and GID of the file with default follow_symlinks")
print("The UID of the filepath is:", os.stat(filepath).st_uid)
print("The GID of the filepath is:", os.stat(filepath).st_gid)
print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid)
print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)

以下是上述代码的输出 -

sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py
The UID of the filepath is: 3000
The GID of the filepath is: 2000
The UID of the symlink_path is: 3000
The GID of the symlink_path is: 2000
changing the UID and GID of the symlink_path file with default follow_symlinks
The UID of the filepath is: 5000
The GID of the filepath is: 5000
The UID of the symlink_path is: 5000
The GID of the symlink_path is: 5000

示例 4

在这里,我们可以看到,当 follow_symlinks = False 时,os.chown() 方法的执行只会修改符号链接文件本身的 UID 和 GID。链接文件的 UID 和 GID 未更改。


import os
filepath = "code.txt"
# creating a symlink for filepath
symlink_path = "code_symlink.txt"
os.symlink(filepath, symlink_path)

# Printing the UID and GID of the path and symlink_path
print("The UID of the path is:", os.stat(filepath).st_uid)
print("The GID of the path is:", os.stat(filepath).st_gid)
print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid)
print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)

# changing the UID and GID of the symlink_path file with follow_symlinks=False
os.chown(symlink_path, 4000, 4000, follow_symlinks = False)
# Printing the UID and GID of the filepath and symlink_path
print("changing the symlink_path UID and GID of the file with follow_symlinks=False")
print("The UID of the path is:", os.stat(filepath).st_uid)
print("The GID of the path is:", os.stat(filepath).st_gid)
print("The UID of the path_symlink is:", os.stat(symlink_path).st_uid)
print("The GID of the path_symlink is:", os.stat(symlink_path).st_gid)

上述代码的输出如下 -

sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py
The UID of the path is: 5000
The GID of the path is: 5000
The UID of the symlink_path is: 5000
The GID of the symlink_path is: 5000
changing the symlink_path UID and GID of the file with follow_symlinks=False
The UID of the path is: 5000
The GID of the path is: 5000
The UID of the path_symlink is: 5000
The GID of the path_symlink is: 5000