简体   繁体   English

如何将文件夹上传到 Google Colab?

[英]How to upload folders to Google Colab?

I want to run a notebook that uses many header files defined in the directory.我想运行一个使用目录中定义的许多 header 文件的笔记本。 So basically I want to upload the entire directory to Google Colab so that I can run the notebook.所以基本上我想将整个目录上传到 Google Colab,以便我可以运行笔记本。 But I am unable to find any such options and only able to upload files not complete folders.但我找不到任何此类选项,只能上传文件而不是完整的文件夹。 So can someone tell me how to upload entire directory to google colab?那么有人可以告诉我如何将整个目录上传到 google colab 吗?

I suggest you not to upload them just in Colab, since when you're restarting the runtime you will lose them (just need to re-upload them, but it can be an issue with very big datasets).我建议你不要只在 Colab 中上传它们,因为当你重新启动运行时你会丢失它们(只需要重新上传它们,但对于非常大的数据集可能是一个问题)。 I suggest you to use the google.colab package to manage files and folders in Colab.我建议您使用google.colab包来管理 Colab 中的文件和文件夹。 Just upload everything you need to your google drive, then import:只需将您需要的所有内容上传到您的谷歌驱动器,然后导入:

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

In this way, you just need to login to your google account through google authentication API, and you can use files/folders as if they were uploaded on Colab.这样,您只需通过谷歌身份验证API登录您的谷歌帐户,您就可以像在Colab上上传文件/文件夹一样使用文件/文件夹。

You can zip them, upload, then unzip it.您可以压缩它们,上传,然后解压缩。

!unzip file.zip !解压缩文件.zip

you can create a git repository and push the files and folders to it, and then can clone the repository in colaboratory with the command您可以创建一个git存储库并将文件和文件夹推送到其中,然后可以与命令合作克隆存储库

!git clone https://github.com/ {username}/{projectname}.git !git clone https://github.com/ {username}/{projectname}.git

i feel this method is faster.我觉得这个方法更快。 but if the file size is more than 100 mb you will have to zip the file or will have to add extentions to push it to github.但如果文件大小超过 100 mb,则必须压缩文件或添加扩展名才能将其推送到 github。 for more information refer the link below.有关更多信息,请参阅下面的链接。

https://help.github.com/en/github/managing-large-files/configuring-git-large-file-storage https://help.github.com/en/github/managing-large-files/configuring-git-large-file-storage

The easiest way to do this, if the folder/file is on your local drive:如果文件夹/文件在您的本地驱动器上,最简单的方法是:

  1. Compress the folder into a ZIP file.将文件夹压缩为 ZIP 文件。
  2. Upload the zipped file into colab using the upload button in the File section.使用文件部分中的上传按钮将压缩文件上传到 colab。 Yes, there is a File section, see the left side of the colab screen .是的,有一个File部分,请参阅colab 屏幕左侧
  3. Use this line of code to extract the file.使用这行代码提取文件。 Note: The file path is from colab's File section.注意:文件路径来自 colab 的文件部分。
from zipfile import ZipFile
file_name = file_path

with ZipFile(file_name, 'r') as zip:
  zip.extractall()
  print('Done')
  1. Click Refresh in the colab File section.单击 colab文件部分中的刷新
  2. Access the files in your folder through the file paths通过文件路径访问文件夹中的文件

Downside: The files will be deleted after the runtime is over.缺点:运行结束后文件将被删除。

You can use some part of these steps if your file is on a Google Drive, just upload the zipped file to colab from Google Drive.如果您的文件在 Google Drive 上,您可以使用这些步骤的一部分,只需将压缩文件从 Google Drive 上传到 colab。

The best way to approach this problem is simple yet tricky sometimes.解决这个问题的最好方法是简单但有时很棘手。

You first need to compress the folder into a zipped file and upload the same into your google drive.您首先需要将该文件夹压缩成一个压缩文件并将其上传到您的谷歌驱动器中。

While doing so, Make sure that the folder is in the root directory of the drive and not in any other subfolder!这样做时,请确保该文件夹位于驱动器的根目录中,而不是在任何其他子文件夹中! . . If the compressed folder/data is in other subfolder, you can easily move the same into the root directory.如果压缩文件夹/数据在其他子文件夹中,您可以轻松地将其移动到根目录中。

Compresses folder/data in another subfolder often messes with the unzipping process when you will be specifying the file location.当您指定文件位置时,压缩另一个子文件夹中的文件夹/数据通常会干扰解压缩过程。

Once you did the afore mentioned tasks, enter the following commands in the colab to mount your drive:完成上述任务后,在 colab 中输入以下命令以挂载驱动器:

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

This will ask for an access token that can be generated by clicking on the url displayed in the output of the same cell这将要求一个访问令牌,该令牌可以通过单击同一单元格输出中显示的 url生成

!ls gdrive/MyDrive

Check the contents of the drive by executing the above command and ensure that your folder/data is displayed in the output.通过执行上述命令检查驱动器的内容,并确保您的文件夹/数据显示在输出中。

!unzip gdrive/MyDrive/<File_name_without_space>.zip

eg:例如:

!unzip gdrive/MyDrive/data_folder.zip

Executing the same will start unzipping your folder into the memory.执行相同的操作将开始将您的文件夹解压缩到内存中。

Congrats!恭喜! You have successfully uploaded your folder/data into the colab.您已成功将文件夹/数据上传到 colab。

zip your files zip -r file.zip your_folder and then: zip 你的文件zip -r file.zip your_folder然后:

from google.colab import files
from zipfile import ZipFile

with ZipFile(files.upload(), 'r') as zip:
  zip.extractall()
  print('Done')

因此,您可以执行以下操作: - 将所需的数据集文件夹上传到您的驱动器 - 通过 colab,安装驱动器,其中“from google.colab import drive drive.mount('/content/gdrive')”会自动显示,您只需需要运行它 - 然后在左侧的 Files 部分检查您的文件(如果文件夹不可见尝试刷新,旁边应该有一个下拉箭头,您可以在其中检查文件夹下的所有文件) - 左键单击获得 COPY PATH 选项的文件夹 - 将复制的路径粘贴到 colab 中的所需位置

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

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