0

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

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

1 回答 1

0

问题是这一行:

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

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

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

strings = re.findall(r'ne\w', uploaded['small.txt'])
于 2018-01-24T21:20:36.023 回答