I am trying to load csv file stored in google drive to colab notebooks. When i try to load the file it is showing "File not found". What is the procedure to load files stored in google drive to colab notebooks??
- 1Please share a notebook illustrating the problem. Keep in mind that loading files from Drive is shown in this example notebook: colab.research.google.com/notebook#fileId=/v2/external/…Bob Smith– Bob Smith2018-01-18 18:34:16 +00:00Commented Jan 18, 2018 at 18:34
- 1df_blend.to_csv('google_data/unlabled_news.csv') on GDrive Google CoLab failed. colab.research.google.com/drive/… [CELL 24]Allen Hurff– Allen Hurff2021-03-31 05:21:28 +00:00Commented Mar 31, 2021 at 5:21
4 Answers
easiest way I found is mounting google drive in colab:
from google.colab import drive drive.mount('/content/gdrive') then use '/content/gdrive/My Drive/' as prefix of the file path. suppose you have a text file in data directory of you google drive. Then you can access it with following code:
open('/content/gdrive/My Drive/data/filename.txt').read() 2 Comments
For accessing file from Google Drive, you need to load file using PyDrive or Drive Rest API.
Run below code before accessing file
!pip install -U -q PyDrive from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive from google.colab import auth from oauth2client.client import GoogleCredentials # 1. Authenticate and create the PyDrive client. auth.authenticate_user() gauth = GoogleAuth() gauth.credentials = GoogleCredentials.get_application_default() drive = GoogleDrive(gauth) # 2. Load a file by ID and create local file. downloaded = drive.CreateFile({'id':'fileid'}) # replace fileid with Id of file you want to access downloaded.GetContentFile('export.csv') # now you can use export.csv 2 Comments
Try:
from google.colab import drive drive.mount('/content/drive') This commands will bring you to a Google Authentication step. You should see a screen with Google Drive File Stream wants to access your Google Account. After you allow permission, copy the given verification code and paste it in the box in Colab.
In the notebook, click on the charcoal > on the top left of the notebook and click on Files. Locate the data folder you created earlier and find your data. Right-click on your data and select Copy Path. Store this copied path into a variable and you are ready to go.
file = "copied path" df = pd.read_csv(file) df.head() TIP: Add a slash (/) as part of the name of directory (for Linux or Mac users). Eg: "/content/drive/My Drive/Colab Notebooks/data/xpto.csv"