[lang-ref] ( get_stem ) ( python )

def test_get_stem():
    # os.path.splitext(path)[0]
    assert os.path.splitext('file1.txt')[0]          == 'file1'
    assert os.path.splitext('file1.tar.gz')[0]       == 'file1.tar'       # Note: only last ext is removed
    assert os.path.splitext('.bashrc')[0]            == '.bashrc'         # Note: . prefix is assumed as a part of stem
    assert os.path.splitext('/path/to/file1.txt')[0] == '/path/to/file1'  # Note: differ from basename command
def test_get_stem_alternative():
    # pathlib.Path.stem / with_suffix('')
    p1 = Path('file1.txt')
    assert p1.stem == 'file1'

    p2 = Path('file1.tar.gz')
    assert p2.stem == 'file1.tar'

    p3 = Path('.bashrc')
    assert p3.suffix == ''

    p4 = Path('/path/to/file1.txt')
    assert p4.stem == 'file1'

    # if you need splitext behavior in path
    assert Path('/path/to/file1.txt').with_suffix('').as_posix() == '/path/to/file1'
    assert Path('/path/to/file1.tar.gz').with_suffix('').as_posix() == '/path/to/file1.tar'