[英]Opening uploaded file in google colaboratory
When I upload and try to open the file using the below code, I am getting this error: 当我上传并尝试使用以下代码打开文件时,出现此错误:
IOError: [Errno 2] No such file or directory
IOError:[Errno 2]没有这样的文件或目录
My code is as follows: 我的代码如下:
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())
The problem is this line: 问题是这一行:
f = open(uploaded['small.txt'], 'r')
When you upload a file using uploaded = files.upload()
, the actual file contents are stored in uploaded['small.txt']
, not the path. 当您使用上
uploaded = files.upload()
上载文件时,实际文件内容存储在上uploaded['small.txt']
,而不是路径中。
So, I believe it should fix the error to simply use uploaded['small.txt']
directly in the regexp, eg, 因此,我认为它应该解决该错误,以便直接在正则表达式中直接使用
uploaded['small.txt']
,例如,
strings = re.findall(r'ne\w', uploaded['small.txt'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.