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


Groups > comp.lang.python > #11424 > unrolled thread

Commands for changing ownership of a file

Started byJason Hsu <jhsu802701@gmail.com>
First post2011-08-14 15:56 -0700
Last post2011-08-15 03:58 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#11424 — Commands for changing ownership of a file

FromJason Hsu <jhsu802701@gmail.com>
Date2011-08-14 15:56 -0700
SubjectCommands for changing ownership of a file
Message-ID<25d3fa2d-15d9-4b85-8f97-e3fa7ccd7b99@q5g2000yqj.googlegroups.com>
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?

[toc] | [next] | [standalone]


#11425

FromChris Rebert <clp2@rebertia.com>
Date2011-08-14 16:37 -0700
Message-ID<mailman.2290.1313365082.1164.python-list@python.org>
In reply to#11424
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

[toc] | [prev] | [next] | [standalone]


#11434

FromMichael Poeltl <michael.poeltl@univie.ac.at>
Date2011-08-15 03:58 +0200
Message-ID<mailman.2295.1313373925.1164.python-list@python.org>
In reply to#11424
in python-3.2.1 I'm using os.system() again, from time to time
maybe that's the one you were looking for?

>>> os.system('chown user:group /tmp/f')
0
>>> os.system('ls -l /tmp/f')
-rw-r--r-- 1 user group 0 Aug 15 03:52 /tmp/f

and besides os.chown() (where you ned the uid and gid), you could also use subprocess.call() or subprocess.Popen()

regards
Michael
* Jason Hsu <jhsu802701@gmail.com> [2011-08-15 01:15]:
> 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?
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

-- 
Michael Poeltl
Computational Materials Physics      voice: +43-1-4277-51409
Univ. Wien, Sensengasse 8/12         fax:   +43-1-4277-9514 (or 9513) 
A-1090 Wien, AUSTRIA   cmp.mpi.univie.ac.at 
--------------------------------------------------------------------------------
slackware-12.2/ubuntu-10.10 | vim-7.3 | python-3.2.1 | mutt-1.5.18 | elinks-0.12
--------------------------------------------------------------------------------

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web