[Tutor] usernames and uid in linux

Bill Campbell bill at celestial.net
Fri Mar 3 19:22:20 CET 2006

On Fri, Mar 03, 2006, Pat Martin wrote:
>   Hello all,
>   I seem to be having a problem finding the right tool in python. Given
>   a username I want to find the uid for it. I can do the reverse very
>   easily with:
>   pwd.getpwuid(2012)
>   This returns a tuple of data from the passwd file. But I want to do
>   something like:
>   command(username)
>   And get something that returns the uid (the number).

You're close:

import pwd

pw = pwd.getpwnam('username')

INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Systems, Inc.
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

There is no distinctly native American criminal class save Congress
    -- Mark Twain

More information about the Tutor mailing list