简体   繁体   English

有没有办法访问本地文件,而无需在Google Colab中使用upload()选项或将数据上传到驱动器然后访问它

[英]Is there a way to access local files without having to use upload() option in Google Colab or uploading the data to the drive and then accessing it

I have data in my local drive spread over a lot of files. 我的本地驱动器中的数据遍布很多文件。 I want to access those data from Google Colab. 我想从Google Colab访问这些数据。 Since it is spread over a large area and the data is susceptible to constant change I don't want to use the upload() option as it can get tedious and long. 由于它分布在一个大的区域,并且数据易受不断变化的影响,我不想使用upload()选项,因为它可能会变得乏味且冗长。 Uploading to Drive is also something I am trying to avoid, due to the changing data values. 由于数据值的变化,我正在努力避免上传到云端硬盘。 So I was wondering if there is another method to access the local data something similar to the code presented. 所以我想知道是否有另一种方法可以访问本地数据,类似于所提供的代码。

def list_files(dir):
    r = []
    for root, dirs, files in os.walk(dir):
        for name in dirs:
            r.append(os.path.join(root, name))
    return r

train_path = list_files('/home/path/to/folder/containing/data/')

This does not seem to work since GC cannot access my local machine. 这似乎不起作用,因为GC无法访问我的本地计算机。 So I always get an empty array (0,) returned from the function 所以我总是从函数返回一个空数组(0,)

The short answer is: no, you can't. 简短的回答是:不,你不能。 The long answer is: you can skip the uploading phase each time you restart the runtime. 答案很长:每次重新启动运行时都可以跳过上传阶段。 You just need to use google.colab package in order to have a similar behaviour to the local environment. 您只需使用google.colab包就可以获得与本地环境类似的行为。 Upload all the files you need to your google drive, then just import: 将您需要的所有文件上传到Google云端硬盘,然后只需导入:

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

After the authentication part, you will be able to access all your files stored in google drive. 在身份验证部分之后,您将能够访问存储在Google云端硬盘中的所有文件。 They will be imported as you have uploaded them, so you just have to modify the last line in this way: 它们将在您上传它们时导入,因此您只需以这种方式修改最后一行:

train_path = list_files('gdrive/path/to/folder/containing/data/')

or in this way: 或以这种方式:

train_path = list_files('/content/gdrive/home/path/to/folder/containing/data/')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM