0

I have a Jupyter notebook that needs access to a .csv data file. I intend to make these accessible for other people in my group, and I want it to work directly for them too, without the need to change the directory, or manually import data, or even 'save shortcut in drive'.

At the moment both files (.ipynb and .csv) are in the same folder on my drive, but when I share the folder, the data file is not accessible to the other people (the pwd is set to /content, and even with mounting the drive, I dont get access to the 'shared with me folder'.

Is there a workaround to allow for direct usage of the .csv file? idealy I just have a cell that copies the csv from the drive to the content folder, but I dont know how to access it:

!cp $where_is_the_folder? data.csv /content

It is very important that the other people don't need to do anything in order to run the notebook.

I also tried the suggestion to use PyDrive and auth as here but it does not seem to work.

At the moment this is the full code, but obviously it only works on my drive, not on others i share it with:

import pandas as pd
from google.colab import drive
drive.mount('/content/drive')
!cp /content/drive/MyDrive/project_folder/data.csv /content
dataFrame = pd.read_csv('/content/data.csv')

note that the notebook and the data.csv are in the same directory (/content/drive/MyDrive/project_folder)

ArieAI
  • 354
  • 1
  • 12

1 Answers1

1

This is mentioned in the post that you linked, but Google Drive has a different structure from a regular filesystem. Your main "My Drive" folder does behave as a traditional folder, but there's no such thing as a "Shared with me folder". "Shared with me" just shows you all the files that you have access to, but they belong to their respective owners' Drives, which you cannot mount in Colab. From your point of view these are like "floating files" which you can only access directly, not from a filesystem.

I don't think you can avoid having to create a shortcut, but with PyDrive you can automate this for your users so they won't have to do it manually. Here's an example:

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

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

source_id = '<ID of your folder or file>'

#Searches first to make sure the shortcut doesn't exist already
flist = drive.ListFile({'q':f'shortcutDetails.targetId="{source_id}" and trashed=false'}).GetList()

if (not flist):
#Create shortcut if file doesn't exist
  shortcut = drive.CreateFile({'mimeType':'application/vnd.google-apps.shortcut', 
                             'shortcutDetails':{
                                 'targetId': source_id
                                 }
                             }).Upload()
else:
  #Do something else

#Shortcut gets created under /content/drive/My Drive/

The shortcut is created by default under the main "My Drive" folder. It essentially works as a symlink and you can treat it as if it were part of the folder structure. This is just a sample but it should help you get going.

References

Daniel
  • 3,157
  • 2
  • 7
  • 15
  • thanks, how do i know the `source_id` ? and will it be the same for all collaborators that use the notebook? – ArieAI Mar 22 '23 at 14:50
  • 1
    The `source_id` is the ID of the Drive file or folder. If you go to a folder on your Drive you can look at the end of the URL and the ID is after `...folders/`. For regular files you can also open them in Drive and check the URL for an ID with a similar format or click the "Get link" option in Drive to get the ID from the link. And yes, the ID of a particular file is unique, so once you get the ID of the file that you want to share it will be the same for everyone. – Daniel Mar 22 '23 at 15:04