简体   繁体   English

如何设置从 Google Colab 到 Google Drive 文件的路径?

[英]How to set the path to Google Drive files from Google Colab?

I am using Google Colab and my code has the following local path to my CSV file in my laptop:我正在使用 Google Colab,我的代码在我的笔记本电脑中有以下本地路径到我的 CSV 文件:

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

I have linked my Google Drive to my notebook in Colab by using the following code:我已使用以下代码将我的 Google Drive 链接到 Colab 中的笔记本:

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

And I have uploaded my CSV file to Google Drive.我已将我的 CSV 文件上传到 Google 云端硬盘。

My question is what to put instead of "C:\\users\\...." to make it work in Google Colab?我的问题是用什么代替"C:\\users\\...."以使其在 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.我已尝试将C:\\Users\\Desktop\\data.csv/gdrive/my drive/Users\\Desktop\\data.csv"但我收到错误消息,指出找不到该文件。

FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError:[Errno 2] 没有这样的文件或目录:

You can browse the contents of your mounted Google Drive folder using:您可以使用以下方式浏览已安装的 Google Drive 文件夹的内容:

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

!ls /content/drive/

which for me outputs:对我来说输出:

MyDrive

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? ).请注意,您应该在路径中的任何地方使用/而不是\ ,因为 Google Colab 是基于 Linux 的系统(请参阅为什么 Windows 对路径使用反斜杠和 Unix 正斜杠? )。 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.基本上,您传递给drive.mount的相同安装路径应该与您在其他任何地方使用的基本路径完全相同

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.因此,如果您将data.csv上传到您的 Google Drive 文件夹( '/content/drive/MyDrive' )的顶层/根目录,那么它也应该显示在/content/drive/MyDrive的顶层/根目录/content/drive/MyDrive目录。

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.我尝试用 /gdrive/my drive/Users\Desktop\data.csv 替换 C:\Users\Desktop\data.csv”,但我收到未找到的错误消息。

Google Drive does not follow the same folder structure as what you have on your local PC. Google Drive 的文件夹结构与您在本地 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直观地查看哪些文件和文件夹可用以及它们的组织方式的最佳方法是在浏览器上打开您的云端硬盘: https://drive.google.com/drive/my-drive

So, for example, if you placed data.csv at My Drive > TEST > DATA:因此,例如,如果您将data.csv放置在 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 :有关在 Colab 中使用 Google Drive 的更多信息,请参阅有关外部数据的教程/文档:本地文件、驱动器、表格和云存储

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.下面的示例展示了如何使用授权代码在运行时挂载您的 Google Drive,以及如何在那里写入和读取文件。 Once executed, you will be able to see the new file ( foo.txt ) at https://drive.google.com/ .执行后,您将能够在https://drive.google.com/看到新文件 ( foo.txt )。

So just @Gino Memphin said it is running in cloud system and uses different path which is totally different compared to Windows Path in your local machine.所以只是@Gino Memphin 说它在云系统中运行并使用不同的路径,这与您本地机器中的 Windows 路径完全不同。 Mount to Google drive and open left panel and go to your file location and click on it.安装到 Google 驱动器并打开左侧面板和 go 到您的文件位置并单击它。 There you will have Copy Path option在那里你将有Copy Path选项

I am going to paste screenshot to clarify it.我将粘贴屏幕截图以澄清它。

在此处输入图像描述

In addition, If you want to run your code inside a specific directory you can make of use this command:此外,如果要在特定目录中运行代码,可以使用以下命令:

%cd path_to_directory

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

"My Drive" <-- See SPACE in name. “我的驱动器” <-- 参见名称中的 SPACE。 All other answers posting wrong "MyDrive" which not work.所有其他发布错误“MyDrive”的答案都不起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 Google Drive 的情况下从 Github 将文件上传到 Colab? - How to upload files to Colab from Github without using Google Drive? 谷歌 colab 错误使用谷歌驱动器中的文件 - Google colab error to use files from google drive 将文件从已安装的 Google Drive 复制到本地 Google Colab session - Copy files from a mounted Google Drive to a local Google Colab session Google Colab 中挂载的 Google Drive 中的相对路径 - Relative path in Google Drive mounted in Google Colab 如何通过 Google Colab notebook (.ipynb) 访问 Google Drive 上的文件? - How to access files on Google Drive via Google Colab notebook (.ipynb)? 如何使用 Google colab 将文件下载到 Google Drive? - How to download files to Google Drive using Google colab? 使用 Google Colab 删除 Google Drive 上的文件 - Remove files on Google Drive using Google Colab Google Colab:如何从我的谷歌驱动器中读取数据? - Google Colab: how to read data from my google drive? 如何将包含多个文件夹的文件夹从谷歌驱动器上传到谷歌 colab - How to upload folder with multiple folders from google drive to google colab 如何正确地将数据从Google云端硬盘导入Google Colab Notebook? - How to properly import data from Google Drive to Google Colab Notebook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM