44

I use Google Colaboratory then I want to save output images in my Google Drive or SSD, HHD but its directory is "/content"

import os     
print(os.getcwd())
# "/content"

so is it possible to change path (HDD, SSD, googledrive)?

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
이성령
  • 1,264
  • 3
  • 19
  • 23

5 Answers5

66

You need to mount google drive to your Colab session.

from google.colab import drive
drive.mount('/content/gdrive')

Then you can simply write to google drive as you would to a local file system like so:

with open('/content/gdrive/My Drive/file.txt', 'w') as f:
  f.write('content')
Tadej Magajna
  • 2,765
  • 1
  • 25
  • 41
23

To save the weight you can run the following after training.

saver = tf.train.Saver()
save_path = saver.save(session, "data/dm.ckpt")
print('done saving at',save_path)

Check the location where the ckpt files were saved.

import os
print( os.getcwd() )
print( os.listdir('data') )

Finally download the file!

from google.colab import files
files.download( "data/dm.ckpt.meta" ) 
Nazmus Sakib
  • 284
  • 1
  • 4
16

One of the other simple methods to save a file to Google Drive I found here is to use the command cp after you mounted the drive.

Here is the code:

from google.colab import drive
drive.mount('/content/gdrive')

Then use this:

!cp -r <CURRENT FILE PATH> <PATH YOU WANT TO SAVE>

Example:

!cp -r './runs/exp0.h5' /content/gdrive/MyDrive/Exp1/
nealmcb
  • 12,479
  • 7
  • 66
  • 91
Prof.Plague
  • 649
  • 10
  • 20
  • 1
    Note that this by default overwrites files if the filename is already used in the target folder. – Chris Hayes Aug 22 '22 at 22:19
  • 1
    @ChrisHayes Yes. That's true, but you can use `-i` option to check it before. It can be found in this [link](https://www.computerhope.com/unix/ucp.htm#:~:text=To%20view%20output%20when%20files,%2Di%20(interactive)%20option). – Prof.Plague Aug 23 '22 at 10:59
  • Thanks - very convenient! But note a bug: you've mounted it on `/content/gdrive`, but copied to `/content/drive`. I assume any subdirectory of `/content/` would work, but you need to change one of them to be consistent. I fixed it to use `/content/gdrive`.... – nealmcb Nov 27 '22 at 04:03
  • @nealmcb Thank you for your attention. I saw both of them and both are the same. I use `/drive` typically but the result is the same. – Prof.Plague Nov 28 '22 at 08:59
7

Take a look at the example on interfacing with external files. The general workflow is to output the file to the cloud environment, then download it.

Let's output the plot from the "Hello, Colaboratory" example to a file. I made a copy of the notebook to my Google Drive and ran the following commands:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(20)
y = [x_i + np.random.randn(1) for x_i in x]
a, b = np.polyfit(x, y, 1)
f = plt.figure()
_ = plt.plot(x, y, 'o', np.arange(20), a*np.arange(20)+b, '-')

f.savefig( "test.png")

If we list the files in the Google Collaboratory environment, we will see test.png among them:

import os
print( os.getcwd() )
print( os.listdir() )
# /content
# ['datalab', '.local', '.config', '.forever', '.cache', '.rnd', 'test.png', '.ipython']

All that's left to do is download it to my local machine using the example I linked at the beginning on this answer:

from google.colab import files
files.download( "test.png" )    

Finally, if you really need the files on Google Drive instead of your local machine, you can use the Google Drive API to move the files accordingly.

P.S. If you don't like writing files to /content, you can always create a subdirectory and os.chdir() into it, but keep in mind that this subdirectory is still local to your cloud environment and requires you to download files as above.

Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
0

If you use ipynb, make sure:

  1. At the begining of your code, you have:
%matplotlib inline
  1. you save it before plt.show(). For instance, the code to save the sns.heatmap of some dataframe df is as follow:
matrix1 = df.corr().round(2)
plt.figure(figsize=(19,16))
sns.heatmap(matrix1, annot=True)
plt.savefig('name_heatmap.png') 
plt.show()

files.download('name_heatmap.png')  
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32647023) – Goran B. Sep 11 '22 at 17:59