Skip to content

Commit

Permalink
Getting home path and username in python
Browse files Browse the repository at this point in the history
  • Loading branch information
hemanth committed May 11, 2013
1 parent e9cb3c9 commit 6fb517f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions user_home.py
@@ -0,0 +1,27 @@
#!/usr/bin/evn python

# Getting the home path:
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']

# Getting user:
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]

0 comments on commit 6fb517f

Please sign in to comment.