Created
July 5, 2012 21:17
-
-
Save brunogama/3056519 to your computer and use it in GitHub Desktop.
Get home path and the current user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### 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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### 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