def replace_ext(filename, ext):
    """
    Return new pathname formed by replacing extension in `filename` with `ext`.
    """
    if ext.startswith('.'):
        ext = ext[1:]
    stem, _ = os.path.splitext(filename)
    return (stem + '.' + ext)