如何在Google Colab中正确加载文本文件

时间:2020-06-08 14:50:11

标签: python machine-learning data-science google-colaboratory

我在Google Colab中上载文本文件时遇到问题。

这是代码

def load_doc(filename):
    # Opening the file as read only
    file = open(filename, 'r')
    text = file.read()
    file.close()
filename = (r'C:\Semicolon\ImageCaptionGenerator\Flickr8k_text\Flickr8k.token.txt')
doc = load_doc(filename)

直接从我的系统中删除了保存到变量filename中的文件路径。因此,我确定没有问题,并且文件夹中存在Flickr8k.token.txt。

这是不断弹出的错误。我尝试使用不同的方法读取不同的文本文件,但不断出现此错误。

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-26-0d5546a08ca7> in <module>()
      5     file.close()
      6 filename = (r'C:\Semicolon\ImageCaptionGenerator\Flickr8k_text\Flickr8k.token.txt')
----> 7 doc = load_doc(filename)

<ipython-input-26-0d5546a08ca7> in load_doc(filename)
      1 def load_doc(filename):
      2     # Opening the file as read only
----> 3     file = open(filename, 'r')
      4     text = file.read()
      5     file.close()

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Semicolon\\ImageCaptionGenerator\\Flickr8k_text\\Flickr8k.token.txt'

我该怎么做才能使这项工作生效。这是我第一次在Google Colab中上传文本文件。请帮助

1 个答案:

答案 0 :(得分:1)

使用笔记本左侧文件浏览器中的文件上传按钮。

以下是屏幕快照,其中突出显示了相关按钮。

enter image description here