69

I found many hints how to upload data into Colaboratory.

But now I want to do opposite -> I want to download .csv I've created in Colaboratory workspace.

How to do this?

F1sher
  • 6,346
  • 9
  • 40
  • 80

8 Answers8

194

Use files colab lib

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

PS: use chrome browser

nidhin
  • 5,783
  • 5
  • 29
  • 45
  • 2
    Also, note that you need to have third-party cookies enabled. Related question [here](https://stackoverflow.com/questions/48420759/upload-local-files-using-google-colab/48427329#48427329) – vladkha Oct 04 '18 at 16:40
  • 4
    Just tested with Firefox 74.0 on Ubuntu - works too – Alexey Tigarev Apr 01 '20 at 22:24
83

You can use the file manager panel.

Use View > Table of contents to show the sidebar then click the Files tab. Right-click the file and select Download.

Google Colab file panel

Note: the process is unusual in that the download progress is not shown in the usual way in the browser. Instead it is shown by an orange circle next to the file in Colab. Only when the download is complete does it appear in the browser downloads.

Google Colab file download progress

In Firefox, it's best to keep the tab in the foreground while the download is in progress as otherwise it can fail.

Tamlyn
  • 18,041
  • 10
  • 96
  • 117
  • 2
    To get there click on view --> table of contents --> Files – max Jun 19 '19 at 10:08
  • 1
    Did this. The file is nowhere to be found, not in my firefox's default download dir, and not under my home dir anywhere, not in Downloads not anywhere else. The file is not in the firefox bin dir or lib dir either. So has anyone actually used this, beyond just imagining how it ought to work. If so please share with us where the file went. – Geoffrey Anderson Aug 29 '19 at 14:42
  • 1
    @GeoffreyAnderson It's buggy on Firefox (as you'd expect from Google these days). As far as I can tell, it works if you keep the focus in that tab when the download-progress-circle to the right of the filename is completing. You don't have to be in the tab the entire time it's downloading, but if you're in a different tab when the download completes its progress, the download doesn't happen, it just vanishes into the aether. – sundar - Remember Monica Nov 03 '19 at 08:17
  • 1
    The appearance is now different. There is no menu labeled "Files". Instead, open the table of contents and click on a folder-like button down along the left margin. Look in `/content` – nealmcb Mar 14 '21 at 20:41
  • It's just as @nealmcb has said but I find the behavior of the download did not change. You still need to keep focus on the tab once it is downloaded otherwise it just vanishes. – Daniel Zaksevski Apr 20 '21 at 12:49
14

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')))
Mohamed Jihad
  • 458
  • 3
  • 5
11

You need to add these two lines:

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

If you are using firefox, then this might not 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.

Ayush Jain
  • 359
  • 3
  • 8
5

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')
Ostap Andrusiv
  • 4,488
  • 1
  • 33
  • 36
2

Faced the same issue in downloading csv from colab in Firefox. Here is a quick workaround (worked for me every time and its wierd).

suppose I have saved a csv like this -

from google.colab import files
submission.to_csv('./submission.csv', sep = ',', index = False)

To download this i first do- try downloading some file which doesnt even exist so that colab gives error

files.download('submission111111.csv')

then run

files.download('submission.csv')

which is the actual file to download. It works everytime for me and I cant stop laughing to be able to find this wierd trick.

Rishabh
  • 181
  • 2
  • 3
0

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

Jatin Balodhi
  • 142
  • 5
  • 18
  • This is doing the wrong thing. OP explicitly wants to get file from colab to home machine, rather than getting file from internet to colab. – nealmcb Mar 14 '21 at 20:38
0

Moving files and folders to Google Drive

  1. Mount google drive - follow instructions output on your screen:
from google.colab import drive
drive.mount('/content/drive')

After this step you'll see extra folder in your sidebar file manager named drive

  1. Copy files either by drag and drop method using sidebar, or with this command (note, need to make sure that the specified folder structure exists on the mounted drive):
# Copying folders, format: !rsync -r --progress source_path destination_path
!rsync -r --progress "./model" "/content/drive/My Drive/Colab Notebooks/my-project/model" 

You can use the same command to move files from Google Drive to notebook environment too, which is a convenient way to backup state in case of runtime disconnects.

ego
  • 2,758
  • 1
  • 24
  • 30