Skip to content

Instantly share code, notes, and snippets.

@leejarvis

leejarvis/.py Secret

Created August 16, 2016 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leejarvis/fb7c4ab6cbe6f85032010cd009aff1d9 to your computer and use it in GitHub Desktop.
Save leejarvis/fb7c4ab6cbe6f85032010cd009aff1d9 to your computer and use it in GitHub Desktop.
def getuser():
"""Get the username from the environment or password database.
First try various environment variables, then the password
database. This works on Windows as long as USERNAME is set.
"""
import os
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
user = os.environ.get(name)
if user:
return user
# If this fails, the exception will "explain" why
import pwd
return pwd.getpwuid(os.getuid())[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment