如何使用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