Commands for changing ownership of a file

J

Jason Hsu

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?
 
C

Chris Rebert

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
 
M

Michael Poeltl

in python-3.2.1 I'm using os.system() again, from time to time
maybe that's the one you were looking for?
-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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top