Python 如何更改目录的所有者
通过使用pwd、grp和os模块,您可以修改文件或目录的所有者。使用uid模块可以获取用户ID,使用组名字符串可以获取组ID,并且可以修改所有者。
以下是使用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并保留另一个不变:
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
极客笔记