3

I am using Google Colab and my code has the following local path to my CSV file in my laptop:

path_csv = "C:\\Users\\Desktop\\data.csv"

I have linked my Google Drive to my notebook in Colab by using the following code:

from google.colab import drive
drive.mount('/content/gdrive')

And I have uploaded my CSV file to Google Drive.

My question is what to put instead of "C:\\users\\...." to make it work in Google Colab?

I have tried replacing C:\\Users\\Desktop\\data.csv with /gdrive/my drive/Users\\Desktop\\data.csv" but I get error message that the file is not found.

FileNotFoundError: [Errno 2] No such file or directory:

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
dimdim dimdim
  • 49
  • 1
  • 1
  • 3
  • I have created in my google drive a folder named newfolder and in there i have put my csv file which is named myfile.csv.So can at least someone tell me how my path looks like?eg: content\gdrive\newfolder\myfile.csv? or sth else? – dimdim dimdim Apr 11 '21 at 02:31

4 Answers4

9

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:

screenshot of My Drive

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/.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • I placed on the top root of my directory. I entered : path_csv = "content\drive\MyDrive\data.csv" with no luck, i still get the same error in the next line where i have : dt=pd.read_csv(path_csv) – dimdim dimdim Apr 11 '21 at 03:04
  • @dimdimdimdim 1st, the paths should have a forward-slash `/`, not ``\``. 2nd, the `/` at the start of the path is important. 3rd, make sure the mount path you passed to `drive.mount` is the same path you use for all other paths (here I used `/content/drive`). – Gino Mempin Apr 11 '21 at 03:10
  • @dimdimdim Whatever you passed as the mount path to the `drive.mount` API, that should be the **same exact base path** you use everywhere else. Here, I'm using `/content/drive`. In your question, you used `'/content/gdrive'`, so it should be `'/content/gdrive/MyDrive/data.csv'` (check with the `!ls` command). – Gino Mempin Apr 11 '21 at 03:17
5

So just as @Gino Mempin said, it is running on a cloud system and it uses a different path, which is totally different compared to Windows paths on your local machine.

Mount the Google Drive and open the left panel and go to your file location and click on it. There you will have a Copy Path option:

enter image description here

In addition, If you want to run your code inside a specific directory you can make use of this command:

%cd path_to_directory
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
user20210310
  • 104
  • 6
0

FYI for Window users, Linux systems such as Google Colab use the "/" seperator instead of "\", so content\drive\MyDrive\data.csv won't work and need to become content/drive/MyDrive/data.csv.

0

"My Drive" <-- See SPACE in name. All other answers posting wrong "MyDrive" which not work.

/content/drive/My Drive/sd-v1-4.ckpt