How to download file created in Colaboratory workspace?

主宰稳场 提交于 2019-11-28 21:17:13

Save it to google drive use Pydrive

# Install the PyDrive wrapper & import libraries.
# This only needs to be done once in a notebook.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once in a notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Create & upload a file.
uploaded = drive.CreateFile({'title': 'filename.csv'})
uploaded.SetContentFile('filename.csv')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

Use files colab lib

from google.colab import files
files.download('example.txt') 

PS: use chrome browser

You can use the file manager panel.

Right-click the file and select "Download". It only lets you view the current folder (and descendants) but if you want a file from elsewhere you can first copy it into the current folder with a shell cell like:

!cp /path/to/file .

Here's an extensive tutorial on how to work with files in Google Colab. If you just want to save your data as csv and download it locally:

from google.colab import files

# e.g. save pandas output as csv
dataframe.to_csv('example.csv')

# or any other file as usual
# with open('example.csv', 'w') as f:
#   f.write('your strings here')

files.download('example.csv')

You need to add these two lines:

from google.colab import files
files.download('file.txt')

If you are using firefox, then this won't work. For making this work:

  1. from google.colab import files
  2. In next cell, print anything, like print('foo').
  3. After it has printed, erase the print line and replace it with: files.download('file.txt')

Now, it will download. This is a hacky solution told by me colleague. I don't know why it works! If you know why, please comment it.

There is a more cleaner and easier way to do this which works in both firefox and chrome.

Click on > icon. Click on files. It will display all the files and folders in your notebook. Left click on the file you want to download, choose download and you are good to go. This procedure can also be applied to upload file/folder. For uploading folder, you would have to zip it first though.

Try this ipython functions. !mkdir data && wget http://file_url/file_name.zip && unzip file.zip -d data/

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