如何使用 Python 更改目錄的所有者?
利用 pwd、grp 和 os 模組,可以修改檔案或目錄的所有者。要從使用者名稱獲取使用者 ID,從組名字串獲取組 ID,以及修改所有者,可以使用 uid 模組。
以下是使用 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 並保持另一個 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
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP