Load local data files to Colaboratory

后端 未结 9 762
野趣味
野趣味 2020-12-02 08:47

I just wondering that is it possible to load local data files(like .xlsx or .csv files that on my google drive) into Colaboratory?

9条回答
  •  孤城傲影
    2020-12-02 09:11

    Putting this out there as an alternative for people who prefer another way to upload more files - this basically allows you to upload your files through Google Drive.

    Run the below code (found this somewhere previously but I can't find the source again - credits to whoever wrote it!):

    !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
    !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
    !apt-get update -qq 2>&1 > /dev/null
    !apt-get -y install -qq google-drive-ocamlfuse fuse
    
    from google.colab import auth
    auth.authenticate_user()
    from oauth2client.client import GoogleCredentials
    creds = GoogleCredentials.get_application_default()
    import getpass
    
    !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
    vcode = getpass.getpass()
    !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
    

    Click on the first link that comes up which will prompt you to sign in to Google; after that another will appear which will ask for permission to access to your Google Drive.

    Then, run this which creates a directory named 'drive', and links your Google Drive to it:

    !mkdir -p drive
    !google-drive-ocamlfuse drive
    

    If you do a !ls now, there will be a directory drive, and if you do a !ls drive you can see all the contents of your Google Drive.

    So for example, if I save my file called abc.txt in a folder called ColabNotebooks in my Google Drive, I can now access it via a path drive/ColabNotebooks/abc.txt

提交回复
热议问题