【问题标题】:Opening uploaded file in google colaboratory在 google colaboratory 中打开上传的文件
【发布时间】:2018-07-03 23:10:07
【问题描述】:

当我上传并尝试使用以下代码打开文件时,我收到此错误:

IOError: [Errno 2] 没有这样的文件或目录

我的代码如下:

from google.colab import files

#upload file
uploaded = files.upload() 

# Open file
f = open(uploaded['small.txt'], 'r')

# Feed the file text into findall(); it returns a list of all the found strings
strings = re.findall(r'ne\w', f.read())

【问题讨论】:

  • 错误出现在哪一行?
  • 感谢您的编辑。错误在第 7 行

标签: python google-colaboratory


【解决方案1】:

问题出在这一行:

f = open(uploaded['small.txt'], 'r')

当您使用uploaded = files.upload() 上传文件时,实际文件内容存储在uploaded['small.txt'] 中,而不是路径中。

所以,我相信它应该可以解决错误,只需在正则表达式中直接使用uploaded['small.txt'],例如,

strings = re.findall(r'ne\w', uploaded['small.txt'])

【讨论】:

  • 很多人对此感到困惑。他们中的大多数人都希望“上传”的文件确实存在于当前文件夹中。它没有按照人们的预期实施是否有原因?是否存在安全原因或其他一些隐藏原因?
猜你喜欢
  • 2021-04-30
  • 2018-08-25
  • 2018-10-14
  • 2018-09-03
  • 1970-01-01
  • 2018-07-06
  • 2019-09-12
  • 2017-07-31
  • 2019-09-17
相关资源
最近更新 更多