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?

Share:
25,655
Jay
Author by

Jay

Updated on April 06, 2020

Comments

  • Jay
    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
    Jason Floyd almost 5 years
    os.chown only available on Unix.
  • Jason Floyd
    Jason Floyd almost 5 years
    takeown /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
    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.