El módulo Shutil en Python proporciona muchas funciones de operaciones de alto nivel en archivos y colecciones de archivos. Viene bajo los módulos de utilidad estándar de Python. Este módulo ayuda a automatizar el proceso de chowning y eliminación de archivos y directorios.
El método shutil.chown() en Python se usa para cambiar el propietario y/o el grupo de la ruta especificada.
Sintaxis: shutil.chown(ruta, usuario = Ninguno, grupo = Ninguno)
Parámetros:
ruta: Un valor de string que representa una ruta válida.
usuario: un valor de string que representa un
grupo de usuarios del sistema: un valor de string que representa un grupo de
usuarios y un grupo también se puede proporcionar mediante la identificación del usuario (uid) y la identificación del grupo (gid), respectivamente.
Tipo de devolución: este método no devuelve ningún valor.
Código #1: uso del método shutil.chown() para cambiar el propietario y el grupo de la ruta especificada
Python3
# 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 specified 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
Código #2: Uso del método shutil.chown()
Python3
# 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 specified 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 specified 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
Referencia: https://docs.python.org/3/library/shutil.html