import local file to google colab

Now google is officially providing support for accessing and working with Gdrive at ease.

You can use the below code to mount your drive to Colab:

from google.colab import drive
drive.mount('/gdrive')
%cd /gdrive/My\ Drive/{location you want to move}

To upload Local files from system to collab storage/directory.

from google.colab import files
def getLocalFiles():
    _files = files.upload()
    if len(_files) >0:
       for k,v in _files.items():
         open(k,'wb').write(v)
getLocalFiles()

enter image description here


To easily upload a local file you can use the new Google Colab feature:

  • click on right arrow on the left of your screen (below the Google Colab logo) enter image description here
  • select Files tab
  • click Upload button

It will open a popup to choose file to upload from your local filesystem.


Colab notebooks are stored on Google Drive. But it is run on another virtual machine. So, you need to copy your data.py there too. Do this to upload data.py through Colab.

from google.colab import files
files.upload()
# choose the file on your computer to upload it then
import data