Firstly is it possible to set a file's owner with python? And if so how do you set a file's owner with python?
chown() method in Python is used to change the owner and group id of the specified path to the specified numeric owner id (UID) and group id (GID). Note: os. chown() method is available only on UNIX platforms and the functionality of this method is typically available only to the superuser or a privileged user.
stat. It gives you st_uid which is the user ID of the owner. Then you have to convert it to the name. To do that, use pwd.
Set file permissions (chmod) To change file permissions, you can use os. chmod(). You can bitwise OR the following options to set the permissions the way you want. These values come from the stat package: Python stat package documentation.
os.chown(path, uid, gid)
http://docs.python.org/library/os.html
The uid and gid can be retrieved from a string by
import pwd import grp import os uid = pwd.getpwnam("nobody").pw_uid gid = grp.getgrnam("nogroup").gr_gid
Reference: How to change the user and group permissions for a directory, by name?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With