bug (resolved) with “Mount drive” web-button in colab. Accessing “shared with me” files from google colab (y2020, previous solutions seem to fail)

前端 未结 2 1177
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 06:45

[ Newer Edit]: colab team reported that they corrected the issue on May 27 2020. I have checked - it works Okay for me now.
Link to issue: https://github.com/googlecolab/col

相关标签:
2条回答
  • 2021-01-23 06:58

    Suppose you want to read a shared csv file from drive. You have done "Add shortcut to Drive".

    1) At Colab Notebook Connect to your drive.

    # Import PyDrive and associated libraries.
    # This only needs to be done once per notebook.
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    from google.colab import auth
    from oauth2client.client import GoogleCredentials
    
    # Authenticate and create the PyDrive client.
    # This only needs to be done once per notebook.
    auth.authenticate_user()
    gauth = GoogleAuth()
    gauth.credentials = GoogleCredentials.get_application_default()
    drive = GoogleDrive(gauth)
    

    2) Get the id of shared file you want to access. Open file -> go to linksharing [https://drive.google.com/open?id=1JKECh3GNry6xbAK6aBSzQtSntD4GTEl ] -> copy the the string after 'id='

    3) back to colab

    # A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz
    file_id = '1JKECh3GNry6xbAK6aBSzQtSntMD4GTEl'
    
    downloaded = drive.CreateFile({'id': file_id}) #important
    print(downloaded['title'])  # it should print the title of desired file
    downloaded.GetContentFile('file.csv')  
    #Finally, you can read the file as pandas dataframe. 
    import pandas as pd
    df= pd.read_csv('file.csv') 
    

    Note : This is my first ever answer to a stack overflow question

    0 讨论(0)
  • 2021-01-23 07:06

    Brief: do NOT mount google drive by web-interface button "Mount drive" (it is bugged), but do it in the "old" command line way, and you will not have problems.

    Details:

    After getting excellent answer above and playing with it, it seems I found some strange thing which results in simpler solution and probably indicates that there is currently a bug with mounting the google drive by the web interface button "Mount drive".

    I mean do NOT mount the drive by interface:

    But do it in the old way:

    and that is all - you will get the access to files which added before with the help of "Add shortcut to Drive":

    0 讨论(0)
提交回复
热议问题