作为代码的一部分,我具有如下功能:
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 path.open('w') as f:
for line in lines:
n_delim = line.count(delim)
matched = delim_pred(n_delim)
if matched:
f.write(line)
n += int(matched)
yield
logger.info(f'Number of {stem} lines: {n}')但是,我得到的属性错误,不能解决它,会感谢任何建议吗?
Traceback (most recent call last):
File "C:/Users/HAXY8W/Desktop/pieter_code_rewriting/main.py", line 95, in <module>
main()
File "C:/Users/HAXY8W/Desktop/pieter_code_rewriting/main.py", line 88, in main
process(
File "C:/Users/HAXY8W/Desktop/pieter_code_rewriting/main.py", line 82, in process
for n_lines, _ in enumerate(zip(*iters)):
File "C:/Users/HAXY8W/Desktop/pieter_code_rewriting/main.py", line 27, in match_output
path = orig_path.with_stem(f'{orig_path.stem}_{stem}')
AttributeError: 'WindowsPath' object has no attribute 'with_stem'我对路径库和斯丁非常陌生,比我聪明的人建议我去调查,所以如果这个问题听起来很新鲜,我很抱歉。
发布于 2021-05-20 01:33:06
path.with_stem()是在Python3.9中引入的。在以前的版本(支持path对象)中,您可以手动执行:
path = orig_path.with_name(f'{orig_path.stem}_{stem}{orig_path.suffix}')https://stackoverflow.com/questions/67600029
复制相似问题