如何下载在Colaboratory工作区中创建的文件?

时间:2018-02-13 19:30:07

标签: google-colaboratory

我发现了很多关于如何将数据上传到Colaboratory的提示。

但现在我想反对 - >我想下载.csv我在Colaboratory工作区创建。

怎么做?

8 个答案:

答案 0 :(得分:59)

使用colab lib文件

from google.colab import files
files.download('example.txt') 

PS:使用chrome浏览器

答案 1 :(得分:21)

您可以使用文件管理器面板。

右键单击文件,然后选择“下载”。它仅允许您查看当前文件夹(及其后代),但是如果要从其他位置访问文件,则可以先使用如下外壳程序单元将其复制到当前文件夹中:

!cp /path/to/file .

Google Colab file panel

答案 2 :(得分:13)

将其保存到谷歌驱动器使用Pydrive

# Install the PyDrive wrapper & import libraries.
# This only needs to be done once in a notebook.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once in a notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Create & upload a file.
uploaded = drive.CreateFile({'title': 'filename.csv'})
uploaded.SetContentFile('filename.csv')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

答案 3 :(得分:4)

关于如何在Google Colab中处理文件的问题,这里为extensive tutorial。 如果您只想将数据另存为var list = [ 'forms/buttons', 'forms/fields', 'layout/common', 'layout/sidebar', 'media/captions', 'media/galleries', 'typography/headings', ]; var needed_order = [ 'typography', 'forms', 'media', 'layout', ]; list.sort((a, b) => needed_order.indexOf(a.split('/')[0]) - needed_order.indexOf(b.split('/')[0])); console.log(list);并在本地下载:

csv

答案 4 :(得分:2)

在Firefox中从colab下载csv时遇到了相同的问题。 这是一个快速的解决方法(每次都为我工作,而且很奇怪)。

假设我已经保存了这样的csv-

from google.colab import files
submission.to_csv('./submission.csv', sep = ',', index = False)

要下载此文件,我首先要做的是- 尝试下载一些甚至不存在的文件,以便colab给出错误消息

files.download('submission111111.csv')

然后运行

files.download('submission.csv')

这是要下载的实际文件。 它对我每次都有效,我不停地笑着就能找到这个奇怪的把戏。

答案 5 :(得分:0)

尝试此ipython函数。 !mkdir data && wget http://file_url/file_name.zip && unzip file.zip -d data/

答案 6 :(得分:0)

您需要添加以下两行:

from google.colab import files
files.download('file.txt')

如果您使用的是Firefox,则此方法将无效。 为了使这项工作:

  1. 从google.colab导入文件
  2. 在下一个单元格中,打印任何内容,例如print('foo')。
  3. 打印后,清除打印行,并替换为:files.download('file.txt')

现在,它将下载。我的同事告诉我,这是一个骇人听闻的解决方案。我不知道为什么会这样!如果您知道原因,请发表评论。

有一种更干净,更轻松的方法可以在Firefox和chrome中使用。

单击>图标。点击文件。它将显示笔记本中的所有文件和文件夹。左键单击要下载的文件,选择“下载”,一切顺利。此过程也可以应用于上传文件/文件夹。对于上传文件夹,您必须先将其压缩。

答案 7 :(得分:0)

将文件和文件夹移动到 Google 云端硬盘

  1. 安装谷歌驱动器 - 按照屏幕上输出的说明进行操作:
from google.colab import drive
drive.mount('/content/drive')

执行此步骤后,您将在侧边栏文件管理器中看到名为 drive

的额外文件夹
  1. 使用侧边栏通过拖放方法或使用此命令复制文件(注意,需要确保安装的驱动器上存在指定的文件夹结构):
# Copying folders, format: !rsync -r --progress source_path destination_path
!rsync -r --progress "./model" "/content/drive/My Drive/Colab Notebooks/my-project/model" 

您也可以使用相同的命令将文件从 Google Drive 移动到笔记本环境,这是一种在运行时断开连接时备份状态的便捷方式。