如何使用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