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
相關用法
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python set()用法及代碼示例
- Python Decimal max()用法及代碼示例
- Python PIL ImageOps.fit()用法及代碼示例
- Python os.rmdir()用法及代碼示例
- Python sympy.det()用法及代碼示例
- Python Decimal min()用法及代碼示例
- Python os.readlink()用法及代碼示例
- Python os.writev()用法及代碼示例
- Python os.readv()用法及代碼示例
- Python PIL RankFilter()用法及代碼示例
- Python os.rename()用法及代碼示例
- Python os.sendfile()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | shutil.chown() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。