首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >路径与行为-归因恐怖

路径与行为-归因恐怖
EN

Stack Overflow用户
提问于 2021-05-19 08:58:08
回答 1查看 1.5K关注 0票数 3

作为代码的一部分,我具有如下功能:

代码语言:javascript
复制
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}')

但是,我得到的属性错误,不能解决它,会感谢任何建议吗?

代码语言:javascript
复制
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'

我对路径库和斯丁非常陌生,比我聪明的人建议我去调查,所以如果这个问题听起来很新鲜,我很抱歉。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-20 01:33:06

path.with_stem()是在Python3.9中引入的。在以前的版本(支持path对象)中,您可以手动执行:

代码语言:javascript
复制
path = orig_path.with_name(f'{orig_path.stem}_{stem}{orig_path.suffix}')
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67600029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档