当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python shutil.chown()用法及代码示例


Python中的Shutil模块提供了许多对文件和文件集合进行高级操作的函数。它属于Python的标准实用程序模块。此模块有助于自动进行文件和目录的删除和删除过程。

shutil.chown()Python中的方法用于更改指定路径的所有者和/或组。

用法: shutil.chown(path, user = None, group = None)

参数:
path:代表有效路径的字符串值。
user:代表系统用户的字符串值
group:代表组的字符串值
用户和组也可以分别由用户ID(uid)和组ID(gid)给出。

返回类型:此方法不返回任何值。

代码1:用于shutil.chown()更改指定路径的所有者和组的方法
# Python program to explain shutil.chown() method  
    
# importing shutil module  
import shutil 
  
# importing Path class of pathlib module  
from pathlib import Path  
  
  
# Path 
path = '/home/ihritik/Desktop/file.txt'
  
  
# Get the owner and group 
# of the specified path 
# using Path.owner() and 
# Path.group() method 
info = Path(path) 
user = info.owner() 
group = info.group() 
  
# Print owner and group 
# of the specified path 
print("Current owner and group of the specified path")  
print("Owner:", user) 
print("Group:", group) 
  
  
# Now, change the owner and group 
# of the specified path 
user = 'ihritik'
group = 'ihritik'
shutil.chown(path, user, group)  
  
print("\nOwner and group changed") 
  
  
# Print the owner and group 
# of the specified path 
info = Path(path) 
user = info.owner() 
group = info.group() 
print("Current owner:", user) 
print("Current group:", group) 
  
  
# Change only group  
# of the specified path 
# and let owner as it is 
group = 'root'
  
shutil.chown(path, group = group)  
  
print("\nOnly group changed") 
  
# Print the owner and 
# group of the speicifed path 
info = Path(path) 
user = info.owner() 
group = info.group() 
print("Current owner:", user) 
print("Current group:", group) 
  
  
# Similarly, we can change 
# only owner of the 
# specified path and let 
# group as it is
输出:
Current owner and group of the specified path
Owner:root
Group:root

Owner and group changed
Current owner:ihritik
Current group:ihritik

Only group changed
Current owner:ihritik
Current group:root
代码2:用于shutil.chown()方法
# Python program to explain shutil.chown() method  
    
# We can also change owner 
# and group of the specified path 
# by passing owner id (uid) and 
# group id (gid) as parameter 
# instead of passing name of  
# owner and / or group  
  
  
# importing shutil module  
import shutil 
  
# importing Path class of pathlib module  
from pathlib import Path  
  
  
# Path 
path = '/home/ihritik/Desktop/file.txt'
  
  
# Get the owner user and 
# group of the speicifed path 
# using Path.owner() and 
# Path.group() method 
info = Path(path) 
user = info.owner() 
group = info.group() 
print("Current owner and group of the specified path")  
print("Current owner:", user) 
print("Current group:", group) 
  
  
# Now, change the owner user 
# and group of the 
# specified path 
  
uid = 0
gid = 0
shutil.chown(path, uid, gid)  
  
print("\nOwner and group changed") 
  
  
# Print the owner user and 
# group of the speicifed path 
info = Path(path) 
user = info.owner() 
group = info.group() 
print("Current owner:", user) 
print("Current group:", group)
输出:
Current owner and group of the specified path
Owner:ihritik
Group:ihritik

Owner and group changed
Current owner:root
Current group:root

参考: https://docs.python.org/3/library/shutil.html



相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | shutil.chown() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。