Pathlib and stem – Attributerror


as part of a code I have function as follow:

def match_output(orig_path: Path,lines: Iterable[str],stem: str, delim: str,delim_pred: Callable[[int], bool],) -> Iterable:
    n = 0
    path = orig_path.with_stem(f'{orig_path.stem}_{stem}')

    with'w') as f:
        for line in lines:
            n_delim = line.count(delim)
            matched = delim_pred(n_delim)
            if matched:

            n += int(matched)
            yield'Number of {stem} lines: {n}')

However, I am getting attribute error, couldn’t solve it, would appreciate any suggestion?

Traceback (most recent call last):
  File "C:/Users/HAXY8W/Desktop/pieter_code_rewriting/", line 95, in <module>
  File "C:/Users/HAXY8W/Desktop/pieter_code_rewriting/", line 88, in main
  File "C:/Users/HAXY8W/Desktop/pieter_code_rewriting/", line 82, in process
    for n_lines, _ in enumerate(zip(*iters)):
  File "C:/Users/HAXY8W/Desktop/pieter_code_rewriting/", line 27, in match_output
    path = orig_path.with_stem(f'{orig_path.stem}_{stem}')
AttributeError: 'WindowsPath' object has no attribute 'with_stem'

I am very new to Pathlib and stem, someone smarter than me recommended that I look into it so apologize if the question sounds newbie


path.with_stem() was introduced in Python 3.9. In previous versions (that support path objects) you can do it manually:

path = orig_path.with_name(f'{orig_path.stem}_{stem}{orig_path.suffix}')
Leave a Reply

Your email address will not be published. Required fields are marked *