Is there anyway I can download the file in Google colaboratory?

我们两清 提交于 2021-02-06 10:11:27

问题


I am trying tensorflow in Google Colaboratory from this codelab, I need to download 'http://download.tensorflow.org/example_images/flower_photos.tgz' this file to complete the lab.

How I can download the file. Is there anyway to upload the tar file without downloading it on my machine.

I tried this method

import urllib
testfile = urllib.URLopener()
testfile.retrieve("http://randomsite.com/file.gz", "file.gz")

This doesn't work, wget is not found also. Anyone please tell me how to do this.


回答1:


Read the manual, they have good examples and explanations: urllib.request

To download:

>>> import os
>>> import urllib.request
>>> urllib.request.urlretrieve('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'google.png')
('google.png', <http.client.HTTPMessage object at 0x7fba3c4cb908>)
>>> os.listdir()
['google.png']

To just check the content:

>>> with urllib.request.urlopen('http://www.python.org/') as f:
...     print(f.read(300))



回答2:


Possibly simpler: use !wget, e.g., executing a cell with the command:

!wget https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

will save the file to the local filesystem of the VM. (Note the leading !. This is a signal to execute the line as a shell command.)



来源:https://stackoverflow.com/questions/49576657/is-there-anyway-i-can-download-the-file-in-google-colaboratory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!