(path)
return userhome + path[i:]
Expand ~ and ~user constructs.

    If user or $HOME is unknown, do nothing.
Expand ~ and ~user constructs.
Expand ~ and ~user constructs .
def expanduser(path):
    """Expand ~ and ~user constructs.

    If user or $HOME is unknown, do nothing."""
    path = os.fspath(path)
    if isinstance(path, bytes):
        tilde = b'~'
    else:
        tilde = '~'
    if not path.startswith(tilde):
        return path
    i, n = 1, len(path)
    while i < n and path[i] not in _get_bothseps(path):
        i += 1

    if 'USERPROFILE' in os.environ:
        userhome = os.environ['USERPROFILE']
    elif not 'HOMEPATH' in os.environ:
        return path
    else:
        try:
            drive = os.environ['HOMEDRIVE']
        except KeyError:
            drive = ''
        userhome = join(drive, os.environ['HOMEPATH'])

    if i != 1: #~user
        target_user = path[1:i]
        if isinstance(target_user, bytes):
            target_user = os.fsdecode(target_user)
        current_user = os.environ.get('USERNAME')

        if target_user != current_user:
            # Try to guess user home directory.  By default all user
            # profile directories are located in the same place and are
            # named by corresponding usernames.  If userhome isn't a
            # normal profile directory, this guess is likely wrong,
            # so we bail out.
            if current_user != basename(userhome):
                return path
            userhome = join(dirname(userhome), target_user)

    if isinstance(path, bytes):
        userhome = os.fsencode(userhome)

    return userhome + path[i:]
def expanduser ( path ) : path = os . fspath ( path ) if isinstance ( path , bytes ) : tilde = b'~' else : tilde = '~' if not path . startswith ( tilde ) : return path i , n = 1 , len ( path ) while i < n and path [ i ] not in _get_bothseps ( path ) : i += 1 if 'USERPROFILE' in os . environ : userhome = os . environ [ 'USERPROFILE' ] elif not 'HOMEPATH' in os . environ : return path else : try : drive = os . environ [ 'HOMEDRIVE' ] except KeyError : drive = '' userhome = join ( drive , os . environ [ 'HOMEPATH' ] ) if i != 1 : #~user target_user = path [ 1 : i ] if isinstance ( target_user , bytes ) : target_user = os . fsdecode ( target_user ) current_user = os . environ . get ( 'USERNAME' ) if target_user != current_user : # Try to guess user home directory.  By default all user # profile directories are located in the same place and are # named by corresponding usernames.  If userhome isn't a # normal profile directory, this guess is likely wrong, # so we bail out. if current_user != basename ( userhome ) : return path userhome = join ( dirname ( userhome ) , target_user ) if isinstance ( path , bytes ) : userhome = os . fsencode ( userhome ) return userhome + path [ i : ]