How to set a files owner in python?
25,655
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?
![Jay](https://i.stack.imgur.com/NhvMD.jpg?s=256&g=1)
Author by
Jay
Updated on April 06, 2020Comments
-
Jay about 4 years
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?
-
Jason Floyd almost 5 yearsos.chown only available on Unix.
-
Jason Floyd almost 5 yearstakeown /d /y only works for English. If you're running German, etc, the 'Y' option isn't correct (J in German, etc). Not a super reliable way to do this.
-
xjcl over 3 years@JasonFloyd Oh jeez. I can't tell which is the worse decision: Making program options locale-dependent, or using Windows for serious development.