Search code examples
pythongoogle-colaboratory

Loading images in google colab


My Jupyter Notebook has the following code to upload an image to Colab:

from google.colab import files
uploaded = files.upload()

I get prompted for the file. Which gets uploaded.

I verify that the file upload was successful using:

!ls

and I see that it's there.

I check the current working directory using:

import os
os.getcwd()

and it tells me that it is /content

now, the following call fails:

assert os.path.exists(img_path)

It also fails whether i'm using just the file name or the full path.

Any thoughts on what is going on?


Solution

  • Use this function to upload files. It will SAVE them as well.

    def upload_files():
      from google.colab import files
      uploaded = files.upload()
      for k, v in uploaded.items():
        open(k, 'wb').write(v)
      return list(uploaded.keys())
    

    Update

    Now (sep 2018), the left pane has a "Files" tab that let you browse files and upload files easily. You can also download by just double click the file names.