(path)
return userhome + path[i:]
Expand ~ and ~user constructions.  If user or $HOME is unknown,
    do nothing.
Expand ~ and ~user constructions.  If user or $HOME is unknown,
    do nothing.
Expand ~ and ~user constructions . If user or $HOME is unknown do nothing .
def expanduser(path):
    """Expand ~ and ~user constructions.  If user or $HOME is unknown,
    do nothing."""
    if isinstance(path, bytes):
        tilde = b'~'
    else:
        tilde = '~'
    if not path.startswith(tilde):
        return path
    sep = _get_sep(path)
    i = path.find(sep, 1)
    if i < 0:
        i = len(path)
    if i == 1:
        if 'HOME' not in os.environ:
            import pwd
            userhome = pwd.getpwuid(os.getuid()).pw_dir
        else:
            userhome = os.environ['HOME']
    else:
        import pwd
        name = path[1:i]
        if isinstance(name, bytes):
            name = str(name, 'ASCII')
        try:
            pwent = pwd.getpwnam(name)
        except KeyError:
            return path
        userhome = pwent.pw_dir
    if isinstance(path, bytes):
        userhome = os.fsencode(userhome)
        root = b'/'
    else:
        root = '/'
    userhome = userhome.rstrip(root) or userhome
    return userhome + path[i:]
def expanduser ( path ) : if isinstance ( path , bytes ) : tilde = b'~' else : tilde = '~' if not path . startswith ( tilde ) : return path sep = _get_sep ( path ) i = path . find ( sep , 1 ) if i < 0 : i = len ( path ) if i == 1 : if 'HOME' not in os . environ : import pwd userhome = pwd . getpwuid ( os . getuid ( ) ) . pw_dir else : userhome = os . environ [ 'HOME' ] else : import pwd name = path [ 1 : i ] if isinstance ( name , bytes ) : name = str ( name , 'ASCII' ) try : pwent = pwd . getpwnam ( name ) except KeyError : return path userhome = pwent . pw_dir if isinstance ( path , bytes ) : userhome = os . fsencode ( userhome ) root = b'/' else : root = '/' userhome = userhome . rstrip ( root ) or userhome return userhome + path [ i : ]