使用`pathlib.Path().glob()`迭代文件时如何将文件名提取为字符串?

问题描述

import pandas as pd
from pathlib import Path

Reports = Path('path/text').glob('*.txt')
for report in Reports:
    raw_text = open(report).read()
    TAB = pd.read_csv('path/tables/*.ann')

我使用 pathlib.Path().glob() 遍历 text 目录下的所有 .txt 文件。对于每一个path/text/file_name.txttables目录下都有一个对应的.ann文件path/tables/file_name.ann(同名文件),我需要这个表来处理原始文本因此。有没有方便的方法打开对应的.ann文件

我可以将每个 .txt 文件文件路径提取为字符串 S,然后

S =                        # extract a path-to-file string from report
file = S.replace('text','tables').replace('txt','ann')
TAB = pd.read_csv(file)

希望有更好的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)