Google colab直接使用Os.listdir访问Machine的本地驱动器 [英] Google colab access Machine's local drives directly using Os.listdir

查看:86
本文介绍了Google colab直接使用Os.listdir访问Machine的本地驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是google colab的新手,我想知道google colab是否能够直接访问计算机cdrive上的文件.

  import os路径='C:\\ Users \\ guest \\ Desktop \\'对于os.listdir(path)中的文件:打印(文件) 

出现的错误消息是[Errno 2]没有这样的文件或目录:'C:\ Users \ zhuan.lim \ Desktop \ script tools \ Python Scripts \'

我在线搜索了一些例子,并说首先使用以下命令上传文件:

google.colab导入文件中的

 上传= files.upload() 

但是,google colab还有另一种方法可以直接从我的驱动器中读取吗​​?

谢谢.

解决方案

您可以通过三种方式使Google Colab访问计算机上的文件:

  1. 将文件上传到Google Colab.

google.colab导入文件中的

 上传= files.upload() 

  1. 将文件上传到您的Google云端硬盘帐户,然后在Colab上安装Google云端硬盘.以我的经验,这是最有用的方法.另外,请注意,这使您可以读写Google云端硬盘(就好像是本地驱动器一样).

google.colab导入驱动器中的

 drive.mount('/content/gdrive')!ls ./content/gdrive 

一旦加载,请单击左窗格上的文件以访问文件结构,如以下屏幕截图所示.

注意:或者,单击文件>>挂载驱动器,这将插入代码段以将Google云端硬盘挂载到您的Colab Notebook中.运行该单元后,您将看到GDrive挂载.

  1. 启动本地运行时,然后访问它.在这种情况下,colab使用您的本地资源,并且本地文件也可以访问.在启动此选项之前,请务必阅读安全注意事项/警告.我还没有亲自尝试过,您在那里就可以自己动手了.

我将在下面解释选项3.

将Colab连接到本地运行时

Colab使您可以连接到本地运行时.如果您已按照

步骤2

点击超链接:

I am new to google colab and i am figuring out if google colab is able to access files on my computer's cdrive directly.

import os
path = 'C:\\Users\\guest\\Desktop\\'

for file in os.listdir(path):
    print(file)

The error message that come out is [Errno 2] No such file or directory: 'C:\Users\zhuan.lim\Desktop\script tools\Python Scripts\'

I searched online and some examples said to upload the files first using:

from google.colab import files
uploaded = files.upload()

However, is there another way for google colab to directly read from my drives?

Thanks in advance.

解决方案

You can make Google Colab access the files on your computer essentially in three ways:

  1. Upload files to Google Colab.

from google.colab import files
uploaded = files.upload()

  1. Upload your files to your Google Drive account and then mount Google Drive on Colab. In my experience, this has been the most helpful method. Also, note that this allows youto both read and write to Google Drive (as if that is a local drive).

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

Once loaded, click on Files on the left pane to access the file-structure, as shown in the following screenshot.

Note: Alternatively, click on Files >> Mount Drive and this will insert the code-snippet to mount Google Drive into your Colab Notebook. Once you run that cell, you will see GDrive getting mounted.

  1. Initiate a local runtime and then access it. In this case colab uses your local resources and the local files are accessible to it as well. Please do read the security concerns/warning before initiating this option. I have not personally tried it and your are on your own there.

I will explain option#3 below.

Connecting Colab to Local Runtime

Colab offers you to connect to a local runtime. If you have installed jupyter_http_over_ws as explained here you should be able to just provide the port you used to start the local runtime and connect to it from colab.

Step-1

Click on Reconnect and then select "Connect to local runtime". (Top right corner in colab).

Step-2

Click on hyperlink: these instructions, in the pop-up as shown below (in step-3), to install jupyter_http_over_ws, if not already installed.

  1. Install and enable the jupyter_http_over_ws jupyter extension (one-time).

pip install jupyter_http_over_ws
jupyter serverextension enable --py jupyter_http_over_ws

  1. Start server and authenticate. New notebook servers are started normally, though you will need to set a flag to explicitly trust WebSocket connections from the Colaboratory frontend.

jupyter notebook \
  --NotebookApp.allow_origin='https://colab.research.google.com' \
  --port=8888 \
  --NotebookApp.port_retries=0

For more details, I encourage you to see these instructions.

Step-3

Provide the correct port number (e.g. 8888) that was used to start the local runtime (jupyter notebook on your local machine).

这篇关于Google colab直接使用Os.listdir访问Machine的本地驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆