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

F1s*_*her 28 google-colaboratory

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

但现在我想做相反的事情 - >我想下载.csv我在Colaboratory工作区创建.

这该怎么做?

nid*_*hin 96

使用文件colab lib

from google.colab import files
files.download('example.txt') 
Run Code Online (Sandbox Code Playgroud)

PS:使用chrome浏览器

  • 谢谢PS.它在Firefox中不起作用. (4认同)
  • 刚刚在 Ubuntu 上使用 Firefox 74.0 进行了测试 - 也可以工作 (4认同)
  • 另请注意,您需要启用第三方 cookie。相关问题[这里](/sf/ask/3389453161/#48427329) (2认同)

Tam*_*lyn 47

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

右键单击该文件,然后选择"下载".它只允许您查看当前文件夹(和后代),但如果您想要其他地方的文件,您可以先将其复制到当前文件夹中,其中包含一个shell单元格:

__PRE__

Google Colab文件面板

  • 要到达那里,请单击视图->目录->文件 (2认同)

Moh*_*had 12

保存到谷歌驱动器使用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')))
Run Code Online (Sandbox Code Playgroud)

  • 保存到GoogleDrive不是问题,而是直接下载到本地计算机 (8认同)

Ayu*_*ain 11

您需要添加这两行:

from google.colab import files
files.download('file.txt')
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 Firefox,那么这可能不起作用。为了使这项工作:

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

现在,它将下载。这是我同事告诉的一个hacky解决方案。我不知道它为什么有效!如果你知道为什么,请评论它。

有一种更干净、更简单的方法可以在 Firefox 和 chrome 中使用。

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


Ost*_*siv 6

下面是一个广泛的教程就如何在谷歌Colab文件。如果您只想将数据另存为csv并在本地下载:

from google.colab import files

# e.g. save pandas output as csv
dataframe.to_csv('example.csv')

# or any other file as usual
# with open('example.csv', 'w') as f:
#   f.write('your strings here')

files.download('example.csv')
Run Code Online (Sandbox Code Playgroud)