#NEWLINE# def expanduser( path ) : #NEWLINE# #INDENT# 'Expand ~ and ~user constructs.\n\n    If user or $HOME is unknown, do nothing.' #NEWLINE# if ( path [ : 1 ] != '~' ) : #NEWLINE# #INDENT# return path #NEWLINE# #UNINDENT# ( i , n ) = ( 1 , len ( path ) ) #NEWLINE# while ( ( i < n ) and ( path [ i ] not in '/\\' ) ) : #NEWLINE# #INDENT# i = ( i + 1 ) #NEWLINE# #UNINDENT# if ( 'HOME' in os . environ ) : #NEWLINE# #INDENT# userhome = os . environ [ 'HOME' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( 'USERPROFILE' in os . environ ) : #NEWLINE# #INDENT# userhome = os . environ [ 'USERPROFILE' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not ( 'HOMEPATH' in os . environ ) ) : #NEWLINE# #INDENT# return path #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# drive = os . environ [ 'HOMEDRIVE' ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# drive = '' #NEWLINE# #UNINDENT# userhome = join ( drive , os . environ [ 'HOMEPATH' ] ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( i != 1 ) : #NEWLINE# #INDENT# userhome = join ( dirname ( path ) , path [ 1 : i ] ) #NEWLINE# #UNINDENT# return ( userhome + path [ i : ] )