如何使用 Python 更改目录的所有者?
通过利用 pwd、grp 和 os 模块,您可以修改文件或目录的所有者。要从用户名获取用户 ID、从组名字符串获取组 ID 以及修改所有者,可以使用 uid 模块。
以下是使用 Python 更改目录所有者的方法 -
使用 os.chown() 方法
要将给定路径的所有者和组 ID 更改为指定的数字所有者 ID (UID) 和组 ID (GID),请使用 Python 的 os.chown() 方法。
语法
os.chown(filepath, uid, gid, *, dir_fd = None, follow_symlinks = True)
其中,
filepath 是要更改其 uid 和 gid 的文件的文件描述符修改。
uid 是一个整数值,表示路径的所有者 id。
gid 是一个数字,表示应为路径输入的组 id(将任何一个 id 设置为 -1 以保持不变),与目录相关的文件描述符称为 dir_fd(它是可选的),其参数的默认值为 None,如果 follow_symlinks则其参数的默认值为 True。如果我们不希望 os.chown() 技术遵循符号链接,我们可以将其设置为 False。如果它返回 False,则该过程将在符号链接而不是它链接到的文件上工作。
注意− 参数列表中的符号"*"表示所有后续参数(在本例中为"dir fd"和"follow symlinks")都是仅关键字参数,只能通过名称提供,而不能作为位置参数提供。
此方法不返回任何值。
示例 - 1(LINUX)
以下是使用 os.chown() 方法更改目录所有者的示例 −
import os path = "C:\Users\Lenovo\Downloads\Work TP\trial.py" print("file's owner id:", os.stat(path).st_uid) print("file's group id:", os.stat(path).st_gid) # Changing the owner id and group id of the file uid = 1500 gid = 1500 os.chown(path, uid, gid) print("\nOwner id and group id of the file is changed succesfully") # Printing the owner id and group id of the file print("\nfile's owner id now is:", os.stat(path).st_uid) print("file's group id now is:", os.stat(path).st_gid)
输出
以下是上述代码的输出 -
└─$ sudo python3 sarika.py file's owner id: 100 file's group id: 100 Owner id and group id of the file is changed successfully file's owner id now is: 1500 file's group id now is: 1500
示例 - 2 (Linux)
以下是设置一个 ID 而保持另一个 ID 不变的示例 −
import os # File path = "C:\Users\Lenovo\Downloads\Work TP\trial.py" # Printing the current owner id and group id print("file's owner id:", os.stat(path).st_uid) print("file's group id:", os.stat(path).st_gid) # Changing only the owner id and leaving the group id unchanged # setting id as -1 to leave the group id unchanged uid = 200 gid = -1 os.chown(path, uid, gid) print("\nOwner id of the file is changed successfully leaving the group id unchanged") # Printing the owner id and group id of the file now print("\nfile's owner id now is:", os.stat(path).st_uid) print("file's group id now is:", os.stat(path).st_gid)
输出
以下是上述代码的输出。
└─$ sudo python3 sarika.py
[sudo] password for sarika:
file's owner id: 1500
file's group id: 1000
Owner id of the file is changed successfully leaving the group id unchanged
file's owner id now is: 200
file's group id now is: 1000
示例 - 3 (Linux)
以下是给定路径为符号链接的示例 -
import os filepath = "C:\Users\Lenovo\Downloads\Work TP\trial.py" # Create a symlink symlink = "C:\Users\Lenovo\Downloads\Work TP\trial(symlink).py" os.symlink(filepath, symlink) print("file's owner id:", os.stat(filepath).st_uid) print("file's group id:", os.stat(filepath).st_gid) print("symlink's owner id:", os.stat(symlink).st_uid) print("symlink's group id:", os.stat(symlink).st_gid) # Changing the owner of the symlink uid = 200 gid = 200 os.chown(symlink, uid, gid) print("\nfile's owner id and group id is changed successfully") # Printing the owner id, the group id and the symlink print("\nfile's owner id:", os.stat(filepath).st_uid) print("file's group id:", os.stat(filepath).st_gid) print("symlink's owner id:", os.stat(symlink).st_uid) print("symlink's group id:", os.stat(symlink).st_gid) # Changing the owner of symlink pointing uid = 400 gid = 400 os.chown(symlink, uid, gid, follow_symlinks = False) print("\n The owner id and group id did not change") # Printing the owner id, the group id and the symlink print("\nfile's owner id:", os.stat(filepath).st_uid) print("file's group id:", os.stat(filepath).st_gid) print("symlink's owner id:", os.stat(symlink).st_uid) print("symlink's group id:", os.stat(symlink).st_gid)
输出
以下是上述代码的输出 -
└─$ sudo python3 code1.py
[sudo] password for govind:
file's owner id: 1000 file's group id: 1000
symlink's owner id: 1000
symlink's group id: 1000
file's owner id and group id is changed successfully
file's owner id: 200
file's group id: 200
symlink's owner id: 200
symlink's group id: 200
The owner id and group id did not change
file's owner id: 200
file's group id: 200
symlink's owner id: 200
symlink's group id: 200
使用shutil.chown()方法
Python Shutil 模块提供了针对文件和文件集合的各种高级操作。它属于 Python 的常用实用程序模块之一。此模块有助于自动执行更改文件所有权和删除目录的过程。
您可以使用shutil.chown()方法修改 Python 中给定路径的所有者和/或组。
语法
shutil.chown(filepath, user = None, group = None)
示例
以下是使用shutil.chown()方法更改文件所有者的示例 -
import shutil from pathlib import Path path = 'C:\Users\Lenovo\Downloads\Work TP\trial.py'# Getting the owner and the user info = Path(path) user = info.owner() group = info.group() print("Present owner and group") print("Present owner:", user) print("Present group:", group) #changing the owner and the group uid = 10 gid = 10 shutil.chown(path, uid, gid) print("\nThe owner and the group is changed successfully") # Printing the owner user and group info = Path(path) user = info.owner() group = info.group() print("Present owner now:", user) print("Present group now:", group)
输出
以下是上述代码的输出 -
$ sudo python3 code2.py
[sudo] password for sarika:
Present owner and group
Present owner: sarika
Present group: sarika
The owner and the group is changed successfully
Present owner now: uucp
Present group now: uucp
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

