當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python os.chown()用法及代碼示例


Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。

如果文件名和路徑無效或無法訪問,或者具有正確類型但操作係統不接受的其他參數,則os模塊中的所有函數都會引發OSError。

os.chown()Python中的方法用於將指定路徑的所有者和組ID更改為指定的數字所有者ID(UID)和組ID(GID)。


注意: os.chown()該方法僅在UNIX平台上可用,並且該方法的函數通常僅對超級用戶或特權用戶可用。

用法: os.chown(path, uid, gid, *, dir_fd = None, follow_symlinks = True) 

參數:
路徑:表示要設置其uid和gid的文件的文件描述符
uid:一個整數值,表示要為路徑設置的所有者ID。
id:一個整數值,表示要為路徑設置的組ID。要使任何一個ID保持不變,請將其設置為-1。
dir_fd(可選):引用目錄的文件描述符。此參數的默認值為“無”。
follow_symlinks(可選):此參數的默認值為True。如果我們不希望os.chown()方法遵循符號鏈接,則可以將其設置為False。如果為False,則方法將對符號鏈接本身進行操作,而不是對鏈接指向的文件進行操作。

注意:參數列表中的“ *”表示以下所有參數(在本例中為“ dir_fd”和“ follow_symlinks”)均為keyword-only參數,可以使用其名稱(而不是位置參數)來提供。

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

代碼1:os.chown()方法的使用
# Python program to explain os.chown() method  
    
# importing os module  
import os 
  
# File path 
path = "./file.txt"
  
# Print the current owner id 
# and group id of the 
# specified file path 
  
# os.stat() method will return a  
# 'stat_result’ object of 
# ‘os.stat_result’ class whose 
# 'st_uid' and 'st_gid' attributes 
# will represent owner id and group id 
# of the file respectively  
print("Owner id of the file:", os.stat(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid)  
  
  
# Change the owner id and  
# the group id of the file 
# using os.chown() method 
uid = 2000
gid = 2000
os.chown(path, uid, gid) 
print("\nOwner and group id of the file changed") 
  
   
# Print the owner id 
# and group id of the file 
print("\nOwner id of the file:", os.stat(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid) 

輸出:
os.chown() method terminal output

代碼2:使用os.chown()方法設置任何一個ID並保持其他不變
# Python program to explain os.chown() method  
    
# importing os module  
import os 
  
# File 
path = "./file.txt"
  
# Print the current owner id 
# and group id of the file 
  
# os.stat() method will return a  
# 'stat_result’ object of 
# ‘os.stat_result’ class whose 
# 'st_uid' and 'st_gid' attributes 
# will represent owner id and group id 
# of the file respectively  
print("Owner id of the file:", os.stat(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid)  
  
  
# Change only owner id of  
# the file and leave 
# group id unchanged 
  
# set id as -1 to leave 
# it unchanged 
uid = 3000
gid = -1
os.chown(path, uid, gid) 
print("\nOwner id of the file changed") 
  
  
# Print the owner id 
# and group id of the file 
print("\nOwner id of the file:", os.stat(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid) 

輸出:
os.chown() method terminal output

代碼3:如果指定的路徑是符號鏈接
# Python program to explain os.chown() method  
    
# importing os module  
import os 
  
# File path 
path = "./file.txt"
  
# Creating a symlink 
# of the above path   
# using os.symlink() method 
symlink = "./file(symlink).txt"
os.symlink(path, symlink) 
  
  
# Print the current owner id 
# and group id of the file 
# as well as the symlink pointing 
# to the above specified file path  
print("Owner id of the file:", os.stat(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid)  
  
print("Owner id of the symlink:", os.stat(symlink).st_uid) 
print("Group id of the symlink:", os.stat(symlink).st_gid)  
  
# Change the owenership  
# of the symlink pointing  
# to the above file './file.txt' 
uid = 1000
gid = 1000
os.chown(symlink, uid, gid) 
print("\nOwner id and group id changed") 
  
  
# Print the owner id 
# and group id of the file 
# as well as the symlink 
print("\nOwner id of the file:", os.stat(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid)  
  
print("Owner id of the symlink:", os.stat(symlink).st_uid) 
print("Group id of the symlink:", os.stat(symlink).st_gid) 
  
  
# As os.chown() method 
# follows symlink 
# so, we can change the 
# owner and group id  
# through a symlink  
  
  
# We can modify the follow_symlinks 
# parameter if we do not 
# want os.chown() method to 
# follow symlink 
  
  
# Change the owenership  
# of the symlink pointing  
# to the above file './file.txt' 
uid = 4000
gid = 4000
os.chown(symlink, uid, gid, follow_symlinks = False) 
print("\nOwner id and group id not changed") 
  
  
# Print the owner id 
# and group id of the file 
# as well as the symlink 
# pointing to it 
print("\nOwner id of the file:", os.stat(path).st_uid) 
print("Group id of the file:", os.stat(path).st_gid)  
  
print("Owner id of the symlink:", os.stat(symlink).st_uid) 
print("Group id of the symlink:", os.stat(symlink).st_gid)

輸出:
os.chown() method terminal output

參考: https://docs.python.org/3/library/os.html



相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.chown() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。