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

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.020
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'subject:changing': 0.07; 'python': 0.08; 'subject:file': 0.13; 'bash,': 0.16; 'chown': 0.16; 'lookup': 0.16; 'root,': 0.16; '\xc2\xa0i': 0.16; 'cc:addr :python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'jason': 0.21; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'gnu': 0.23; 'pm,': 0.24; 'command': 0.24; 'aug': 0.24; 'ownership': 0.29; 'message-id:@mail.gmail.com': 0.29; 'script': 0.29; 'cc:addr:python.org': 0.30; 'sun,': 0.30; 'equivalent': 0.31; 'modules': 0.31; 'url:library': 0.31; 'chris': 0.32; "what's": 0.33; 'there': 0.33; 'instead': 0.33; 'uses': 0.35; 'url:python': 0.36; 'group,': 0.36; 'created': 0.36; 'but': 0.37; 'something': 0.37; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'url:docs': 0.39; 'username': 0.77; 'sender:addr:chris': 0.84; 'received:209.85.218.46': 0.91; 'received:mail- yi0-f46.google.com': 0.91; 'username.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=XvF8oV2O7Evwn8qVVucbvs/QPegj8H1hF/pHsEF2M/U=; b=AVovpEv1eye7M/6McjBIrI/tHUZcK3lCRjur3yL8ABUMI3k0oHwQZ7eG3wuNeuLYxY cEDNT/u8edvt3HqCIW2ZUKJYob+xuVDkoK1EG/Q/diygvSwLQBPNatBCqrdr9BcNMKQq kss5MAInkzafzcuHS1M96FE2KWUJqylXuMfPU=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <25d3fa2d-15d9-4b85-8f97-e3fa7ccd7b99@q5g2000yqj.googlegroups.com>
References <25d3fa2d-15d9-4b85-8f97-e3fa7ccd7b99@q5g2000yqj.googlegroups.com>
Date Sun, 14 Aug 2011 16:37:57 -0700
X-Google-Sender-Auth W-s_bKRk09LPToAoyQ8EOH2rpWg
Subject Re: Commands for changing ownership of a file
From Chris Rebert <clp2@rebertia.com>
To Jason Hsu <jhsu802701@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2290.1313365082.1164.python-list@python.org> (permalink)
Lines 17
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313365082 news.xs4all.nl 23939 [2001:888:2000:d::a6]:57489
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11425

Show key headers only | 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