Commands for changing ownership of a file

Chris Rebert clp2 at rebertia.com
Sun Aug 14 19:37:57 EDT 2011


On Sun, Aug 14, 2011 at 3:56 PM, Jason Hsu <jhsu802701 at gmail.com> wrote:
> I have a script that I execute as root, but I need to change the
> ownership of the files created in the script to that of my username.
> In GNU Bash, the command is something like "chown myusername:users".
> What's the equivalent Python command?  I know that there is a command
> that uses numbers for the username and group, but is there a command
> that would allow me to use "myusername" and "users" instead of numbers?

Simply use the `pwd` and `grp` modules to lookup the uid and gid for
the username and group-name respectively. Then use the id-based chown
function(s) you already came across.
http://docs.python.org/library/pwd.html#pwd.getpwnam
http://docs.python.org/library/grp.html#grp.getgrnam

Cheers,
Chris



More information about the Python-list mailing list