正在 google colaboratory 打开上传的文件

Opening uploaded file in google colaboratory

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

IOError: [Errno 2] No such file or directory

我的代码如下:

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())

问题出在这一行:

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

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

所以,我相信它应该修复直接在正则表达式中直接使用 uploaded['small.txt'] 的错误,例如

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