In Python under Linux, what is the easiest way to check the existence of a user, given his/her login?
Anything better than issuing 'ls ~login-name' and checking the exit code?
And if running under Windows?
In Python under Linux, what is the easiest way to check the existence of a user, given his/her login?
Anything better than issuing 'ls ~login-name' and checking the exit code?
And if running under Windows?
To look up my userid (bagnew) under Unix:
import pwd
pw = pwd.getpwnam("bagnew")
uid = pw.pw_uid
See the pwd module info for more.
I would parse /etc/passwd for the username in question. Users may not necessarily have homedir's.
Using pwd you can get a listing of all available user entries using pwd.getpwall(). This can work if you do not like try:/except: blocks.
import pwd
userid = "zomobiba"
if userid in [x[0] for x in pwd.getpwall()]:
print "Yay"