Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11425

Re: Commands for changing ownership of a file

References <25d3fa2d-15d9-4b85-8f97-e3fa7ccd7b99@q5g2000yqj.googlegroups.com>
Date 2011-08-14 16:37 -0700
Subject Re: Commands for changing ownership of a file
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.2290.1313365082.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Aug 14, 2011 at 3:56 PM, Jason Hsu <jhsu802701@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Commands for changing ownership of a file Jason Hsu <jhsu802701@gmail.com> - 2011-08-14 15:56 -0700
  Re: Commands for changing ownership of a file Chris Rebert <clp2@rebertia.com> - 2011-08-14 16:37 -0700
  Re: Commands for changing ownership of a file Michael Poeltl <michael.poeltl@univie.ac.at> - 2011-08-15 03:58 +0200

csiph-web