You can browse the contents of your mounted Google Drive folder using:
from google.colab import drive
drive.mount('/content/drive')
!ls /content/drive/
which for me outputs:
MyDrive
For some users, this outputs "My Drive" (with a space). Take note of what's the actual output on your system, and apply it accordingly to the rest of my answer.
Going further:
!ls -l /content/drive/MyDrive/
which for me outputs:
total 41
drwx------ 2 root root 4096 May 4 2017 ...
-rw------- 1 root root 18 Apr 11 02:37 data.csv
...
Note that you should be using /
instead of a \
everywhere in your paths, as Google Colab is a Linux-based system (see Why does Windows use backslashes for paths and Unix forward slashes?). Also, the /
at the start of the path is important. Basically, the same mount path you passed to drive.mount
should be exactly the same base path you use everywhere else.
So, if you uploaded your data.csv
at the top-level/root of your Google Drive folder ('/content/drive/MyDrive'
), then it should also show up there at the top-level/root of the /content/drive/MyDrive
directory.
path_to_csv = '/content/drive/MyDrive/data.csv'
with open(path_to_csv) as f:
for line in f.read().splitlines():
print(line)
I have tried replacing C:\Users\Desktop\data.csv with /gdrive/my drive/Users\Desktop\data.csv" but i get error message that is not found.
Google Drive does not follow the same folder structure as what you have on your local PC. The best way to visually see which files and folders are available and how they are organized is to open your Drive on your browser at: https://drive.google.com/drive/my-drive
So, for example, if you placed data.csv
at My Drive > TEST > DATA:

Then the corresponding path would be:
# Check the root folder
!ls /content/drive/
# Path should be visually same as in drive.google.com
!ls /content/drive/MyDrive/TEST/DATA
path_to_csv = '/content/drive/MyDrive/TEST/DATA/data.csv'
MyDrive
data.csv
For more information on working with Google Drive in Colab, see the tutorial/docs on External data: Local Files, Drive, Sheets, and Cloud Storage:
The example below shows how to mount your Google Drive on your runtime using an authorization code, and how to write and read files there. Once executed, you will be able to see the new file (foo.txt
) at https://drive.google.com/.