当我上传并尝试使用以下代码打开文件时,我收到此错误:
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())
答案 0 :(得分: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'])