Return to Snippet

Revision: 7290
at July 17, 2008 06:49 by scarfboy


Updated Code
homedir = os.path.expanduser('~')

# ...works on at least windows and linux. 
# In windows it points to the user's folder 
#  (the one directly under Documents and Settings, not My Documents)


# In windows, you can choose to care about local versus roaming profiles.
# You can fetch the current user's through PyWin32.
#
# For example, to ask for the roaming 'Application Data' directory:
#  (CSIDL_APPDATA asks for the roaming, CSIDL_LOCAL_APPDATA for the local one)
#  (See microsoft references for further CSIDL constants)
try:
    from win32com.shell import shellcon, shell            
    homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
 
except ImportError: # quick semi-nasty fallback for non-windows/win32com case
    homedir = os.path.expanduser("~")

Revision: 7289
at July 17, 2008 06:49 by scarfboy


Updated Code
os.path.expanduser('~')

# ...works on at least windows and linux. 
# In windows it points to the user's folder 
#  (the one directly under Documents and Settings, not My Documents)


# In windows, you can choose to care about local versus roaming profiles.
# You can fetch the current user's through PyWin32.
#
# For example, to ask for the roaming 'Application Data' directory:
#  (CSIDL_APPDATA asks for the roaming, CSIDL_LOCAL_APPDATA for the local one)
#  (See microsoft references for further CSIDL constants)
try:
    from win32com.shell import shellcon, shell            
    datapath = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
 
except ImportError: # quick semi-nasty fallback for non-windows/win32com case
    datapath = os.path.expanduser("~")

Revision: 7288
at July 17, 2008 06:48 by scarfboy


Initial Code
os.path.expanduser('~')

# ...works on at least windows and linux. 
# In windows it points to the user's folder 
#  (the one directly under Documents and Settings, not My Documents)

# In windows, you can choose to care about local versus roaming profiles.
# You can fetch the current user's through PyWin32. For example, to
# ask for the roaming 'Application Data' directory:
try:
     from win32com.shell import shellcon, shell            
     datapath = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
 
 except ImportError: # quick semi-nasty fallback for non-windows/win32com case
     datapath = os.path.expanduser("~")

# (CSIDL_APPDATA asks for the roaming, CSIDL_LOCAL_APPDATA for the local one)
# See microsoft references for further CSIDL constants

Initial URL


Initial Description
The current user's home directory is probably the easiest way to get a writeable directory, and expanduser does the sensible thing on various OSes. 

In case of windows, you can choose to be smarter about *which* directory you pick.

Initial Title
Get Home directory path  in Python (win, lin, other?)

Initial Tags


Initial Language
Python