Python os.chown() 方法



Python os.chown() 方法将路径的所有者和组 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 − 这是要为文件设置的所有者 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

示例 2

默认情况下,`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

示例 3

在这里,我们可以看到,当 `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
os_file_methods.htm
广告