没有这样的文件或目录:'train_art_path'

No such file or directory: 'train_art_path'

我正在尝试使用 Jupyter 笔记本从文件路径读取文档,如下所示。

train_art_path = "/work/TEXT_SUMMARIZATION/train_dialogue.txt"
with open(train_art_path, 'r') as file:
    a=file.readlines()
    print(a)
    print('\n')

我遇到以下问题:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
/tmp/ipykernel_79659/545421924.py in <module>
----> 1 with open(train_art_path, 'r') as file:
      2     a=file.readlines()
      3     print(a)
      4     print('\n')

FileNotFoundError: [Errno 2] No such file or directory: 'train_art_path'

或者,我尝试如下阅读文档。它完美运行。

with open('train_dialogue.txt', 'r') as file:
    a=file.readlines()
    print(a)
    print('\n')

你能帮我解决第一个问题吗?提前致谢。

您正在使用 train_art_path 作为变量,因此赋值后不应有引号 '。改变...

with open('train_art_path', 'r') as file:

with open(train_art_path, 'r') as file: