Python os.chown() 方法



Python os.chown() 方法將路徑的所有者和組 ID 更改為指定的 uid 和 gid。要保留其中一個 ID 不變,請將 uid 或 gid 設定為 -1。要執行修改操作,需要超級使用者許可權。

為了確定使用者或組可以訪問哪些系統資源,類 Unix 作業系統使用稱為使用者識別符號 (UID) 的值來標識使用者,使用組識別符號 (GID) 來標識組。

注意:此方法僅在 UNIX/LINUX 平臺上可用。

語法

以下是Python os.chown() 方法的語法:

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

引數

  • path − 這是需要設定所有者 ID 和組 ID 的路徑。

  • uid − 這是要為檔案設定的所有者 ID。

  • gid − 這是要為檔案設定的組 ID。

  • dir_fd − 指向目錄的檔案描述符。預設為 None。

  • follow_symlinks − 預設為 True。當為 False 時,該方法將運算子號連結本身,而不是連結指向的檔案。

注意:引數列表中的 * 表示所有後續引數都是關鍵字引數(在本例中為 dir_fd 和 follow_symlinks),即非位置引數。

返回值

此方法不返回值。

示例 1

如果我們不想更改 gid,可以將其設定為 -1。

以下示例演示了 Python os.chown() 方法的使用方法。此處,檔案的擁有權更改為給定的 uid '100' 和 gid '-1'。因此,僅修改了 uid。

import os, sys
# Assuming /tmp/foo.txt exists.
# To set owner ID 100 following has to be done.
os.chown("/tmp/foo.txt", 100, -1)
print ("Changed ownership successfully!!")

執行上述程式時,會產生以下結果:

Changed ownership successfully!!

示例 2

在這裡,我們嘗試更改檔案路徑的 uid 和 gid。給定的 uid 為 '3000',gid 為 '2000'。

# importing the module
import os
filepath = "code.txt"
# Printing the current UID and GID of the file
print("The current UID of the file is:", os.stat(filepath).st_uid)
print("The current GID of the file is:", os.stat(filepath).st_gid)
# Using the method
os.chown(path, 3000, 2000)
print("\n The UID and GID of this file has been changed succesfully")
# Printing the UID and GID of the file after modifying
print("\n The modified uid of the file is:", os.stat(filepath).st_uid)
print("The modified gid of the file is:", os.stat(filepath).st_gid)

執行上述程式碼時,我們得到以下輸出:

sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py
The current UID of the file is: 1000
The current GID of the file is: 1000

 The UID and GID of this file has been changed succesfully

 The modified uid of the file is: 3000
The modified gid of the file is: 2000

示例 2

預設情況下,`follow_symlink` 為 True。當設定為 False 時,該方法將運算子號連結本身,而不是符號連結指向的檔案。

在下面的示例中,建立了給定檔案路徑 "code.txt" 的符號連結。然後我們嘗試更改指向檔案路徑的符號連結的 uid 和 gid。這裡使用了預設的 `follow_symlink`。

import os
filepath = "code.txt"
# creating a symlink for filepath
symlink_path = "code_symlink.txt"
os.symlink(filepath, symlink_path)

# Printing the UID and GID of the filepath and symlink_path
print("The UID of the filepath is:", os.stat(filepath).st_uid)
print("The GID of the filepath is:", os.stat(filepath).st_gid)
print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid)
print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)

# changing the symlink_path UID and GID of the file with default follow_symlinks
os.chown(symlink_path, 5000, 5000)
# Printing the UID and GID of the filepath and symlink_path
print("changing the symlink_path UID and GID of the file with default follow_symlinks")
print("The UID of the filepath is:", os.stat(filepath).st_uid)
print("The GID of the filepath is:", os.stat(filepath).st_gid)
print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid)
print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)

以下是上述程式碼的輸出:

sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py
The UID of the filepath is: 3000
The GID of the filepath is: 2000
The UID of the symlink_path is: 3000
The GID of the symlink_path is: 2000
changing the UID and GID of the symlink_path file with default follow_symlinks
The UID of the filepath is: 5000
The GID of the filepath is: 5000
The UID of the symlink_path is: 5000
The GID of the symlink_path is: 5000

示例 3

在這裡,我們可以看到,當 `follow_symlinks = False` 時,`os.chown()` 方法的執行只會修改符號連結檔案本身的 UID 和 GID。連結檔案的 UID 和 GID 沒有改變。

import os
filepath = "code.txt"
# creating a symlink for filepath
symlink_path = "code_symlink.txt"
os.symlink(filepath, symlink_path)

# Printing the UID and GID of the path and symlink_path
print("The UID of the path is:", os.stat(filepath).st_uid)
print("The GID of the path is:", os.stat(filepath).st_gid)
print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid)
print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)

# changing the UID and GID of the symlink_path file with follow_symlinks=False
os.chown(symlink_path, 4000, 4000, follow_symlinks = False)
# Printing the UID and GID of the filepath and symlink_path
print("changing the symlink_path UID and GID of the file with follow_symlinks=False")
print("The UID of the path is:", os.stat(filepath).st_uid)
print("The GID of the path is:", os.stat(filepath).st_gid)
print("The UID of the path_symlink is:", os.stat(symlink_path).st_uid)
print("The GID of the path_symlink is:", os.stat(symlink_path).st_gid)

上述程式碼的輸出如下:

sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py
The UID of the path is: 5000
The GID of the path is: 5000
The UID of the symlink_path is: 5000
The GID of the symlink_path is: 5000
changing the symlink_path UID and GID of the file with follow_symlinks=False
The UID of the path is: 5000
The GID of the path is: 5000
The UID of the path_symlink is: 5000
The GID of the path_symlink is: 5000
os_file_methods.htm
廣告
© . All rights reserved.