Daniel James
Daniel James

Reputation: 1433

How to upload csv file into google drive and read it from same into python

I have a google drive which I have my csv file uploaded in already, the link to share that file is given as:

https://drive.google.com/open?id=1P_UYUsgvGXUhPCKQiZWlEAynKoeldWEi

I also know my the directory to the drive as:

C:/Users/.../Google Drive/

Please give me a step-by-step guide to achieving how to read this particular csv file directly from google drive and not by downloading it to my PC first before reading it to python.

I have searched this forum and tried some given solutions such as:

How to upload csv file (and use it) from google drive into google colaboratory

It did not work for me, it resulted to the below error:

      3 from pydrive.auth import GoogleAuth
      4 from pydrive.drive import GoogleDrive
----> 5 from google.colab import auth
      6 from oauth2client.client import GoogleCredentials
      7 

ModuleNotFoundError: No module named 'google.colab'

Upvotes: 1

Views: 13009

Answers (2)

Christopher Kinyua
Christopher Kinyua

Reputation: 170

Here is a simple approach I use for all my csv files stored in Google Drive.

First import the necessary libraries that will facilitate your connection.

  !pip install -U -q PyDrive

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

Next step is authentication and creating the PyDrive client in order to connect to your Drive.

This should give you a link to connect to Google Cloud SDK.

Select the Google Drive account you want to access. Copy the link and paste it onto the text field prompt on your Colab Notebook.

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

To get the file, you will need the id of the file in Google Drive.

downloaded = drive.CreateFile({'id':'1P_UYUsgvGXUhPCKQiZWlEAynKoeldWEi'}) # replace the id with id of the file you want to access
downloaded.GetContentFile('file.csv')  

Finally, you can read the file as pandas dataframe.

import pandas as pd
df= pd.read_csv('fle.csv') 

Upvotes: 2

lovely_santa
lovely_santa

Reputation: 151

You don't need that much out of that example to upload a file to google drive:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

# access the drive
gauth = GoogleAuth()
drive = GoogleDrive(gauth)

# the file you want to upload, here simple example
f = drive.CreateFile()
f.SetContentFile('document.txt')

# upload the file
f.Upload()
print('title: %s, mimeType: %s' % (f['title'], f['mimeType']))

# read all files, the newly uploaded file will be there
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
  print('title: %s, id: %s' % (file1['title'], file1['id']))

Note: I created an empty file in this example instead of an existing one, you just have to change it to load up the csv file from your local pc where the python file is running on instead.

Kind regards

Upvotes: 1

Related Questions