Tell if a given login exists in Linux using Python
Question: 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?


Answer: This answer builds upon the [answer by
Brian](http://stackoverflow.com/a/2540487/832230). It adds the necessary
`try...except` block.

Check if a user exists:

    
    
    import pwd
    
    try:
        pwd.getpwnam('someusr')
    except KeyError:
        print('User someusr does not exist.')
    

Check if a group exists:

    
    
    import grp
    
    try:
        grp.getgrnam('somegrp')
    except KeyError:
        print('Group somegrp does not exist.') 
    

