1

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'
Daniel James
  • 1,381
  • 1
  • 10
  • 28

2 Answers2

1

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

lovely_santa
  • 151
  • 1
  • 10
  • Thanks for your response. I have created my working directory as: os.chdir("C:/Users/.../Google Drive/Python Stuff"). The name of the file is: "myfile.csv" Should I change "document.txt" to "myfile.csv" or "C:/Users/.../Google Drive/Python Stuff/myfile.csv"? What other variable(s) will I change in the code you provided? – Daniel James Oct 14 '18 at 18:07
  • The f variable is a file like entity, you can use some extra packages like 'os' to get the absolute path, or temporary make a copy to the python folder. The 'os' package should be enough like you said before. And no, that should be the only thing you have to change... the other stuff is only reading the drive... – lovely_santa Oct 14 '18 at 19:34
  • InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2) – Daniel James Oct 14 '18 at 19:40
1

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')