google colab read file from drive code example

Example 1: colab mount drive

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

#Optional: move to the desired location:
%cd drive/My Drive/DIRECTORY_IN_YOUR_DRIVE

Example 2: colab read file

from google.colab import files
import io
uploaded = files.upload()
df2 = pd.read_csv('Filename.csv')# Dataset is now stored in a Pandas Dataframe

Example 3: download google drive file colab

!gdown --id yourFileIdHere

Example 4: read file from google drive colab

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

Example 5: read file from google drive colab

downloaded = drive.CreateFile({'id':"your_file_ID"})   # replace the id with id of file you want to access
downloaded.GetContentFile('your_file_name.csv')        # replace the file name with your file

Example 6: read file from google drive colab

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)