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

mpen picture mpen · May 13, 2011 · Viewed 55.7k times · Source

os.chown is exactly what I want, but I want to specify the user and group by name, not ID (I don't know what they are). How can I do that?

Answer

Diego Torres Milano picture Diego Torres Milano · May 13, 2011
import pwd
import grp
import os

uid = pwd.getpwnam("nobody").pw_uid
gid = grp.getgrnam("nogroup").gr_gid
path = '/tmp/f.txt'
os.chown(path, uid, gid)