Skip to content

Instantly share code, notes, and snippets.

@brunogama
Created July 5, 2012 21:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunogama/3056519 to your computer and use it in GitHub Desktop.
Save brunogama/3056519 to your computer and use it in GitHub Desktop.
Get home path and the current user
##### get home path
##### src: http://h3manth.com/content/getting-home-path-and-username-python
import os
home = os.curdir
if 'HOME' in os.environ:
home = os.environ['HOME']
elif os.name == 'posix':
home = os.path.expanduser("~/")
elif os.name == 'nt':
if 'HOMEPATH' in os.environ and 'HOMEDRIVE' in os.environ:
home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
else:
home = os.environ['HOMEPATH']
##### get the current user
##### src: http://h3manth.com/content/getting-home-path-and-username-python
import os
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
user = os.environ.get(name)
if user:
return user
# If not user from os.environ.get()
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