There are many ways to read the files in your colab notebook(**.ipnb), a few are:
- Mounting your Google Drive in the runtime's virtual machine.here &, here
- Using google.colab.files.upload(). the easiest solution
- Using the native REST API;
- Using a wrapper around the API such as PyDrive
Method 1 and 2 worked for me, rest I wasn't able to figure out. If anyone could, as others tried in above post please write an elegant answer. thanks in advance.!
First method:
I wasn't able to mount my google drive, so I installed these libraries
# Install a Drive FUSE wrapper.
# https://github.com/astrada/google-drive-ocamlfuse
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
Once the installation & authorization process is finished, you first mount your drive.
!mkdir -p drive
!google-drive-ocamlfuse drive
After installation I was able to mount the google drive, everything in your google drive starts from /content/drive
!ls /content/drive/ML/../../../../path_to_your_folder/
Now you can simply read the file from path_to_your_folder
folder into pandas using the above path.
import pandas as pd
df = pd.read_json('drive/ML/../../../../path_to_your_folder/file.json')
df.head(5)
you are suppose you use absolute path you received & not using /../..
Second method:
Which is convenient, if your file which you want to read it is present in the current working directory.
If you need to upload any files from your local file system, you could use below code, else just avoid it.!
from google.colab import files
uploaded = files.upload()
for fn in uploaded.keys():
print('User uploaded file "{name}" with length {length} bytes'.format(
name=fn, length=len(uploaded[fn])))
suppose you have below the folder hierarchy in your google drive:
/content/drive/ML/../../../../path_to_your_folder/
Then, you simply need below code to load into pandas.
import pandas as pd
import io
df = pd.read_json(io.StringIO(uploaded['file.json'].decode('utf-8')))
df