How to set a files owner in python? How to set a files owner in python? python python

How to set a files owner in python?


os.chown(path, uid, gid)

http://docs.python.org/library/os.html

The uid and gid can be retrieved from a string by

import pwdimport grpimport osuid = pwd.getpwnam("nobody").pw_uidgid = grp.getgrnam("nogroup").gr_gid

Reference: How to change the user and group permissions for a directory, by name?


Old, but might help in the future for those who wish to set files owner in windows.

*I have yet to find a pure 'pythonic' method, this is the alternative:

Windows provides the following takeown.exe utility which we will take advantage of:

takeown /f folder_path /r /d Y ('r' for recursively take ownership on all files and folders in the tree and 'd' for default input parameter that will allow to take ownership on all files\folders). further documentation : msdn docs

Code sample:

from subprocess import STDOUT, check_outputcheck_output(["takeown", "/f", path_, "/r", "/d", "Y"], stderr=STDOUT)