【问题标题】:How to change where in Google Drive a Colab notebook is saved?如何更改 Colab 笔记本在 Google Drive 中的保存位置?
【发布时间】:2023-02-22 12:18:16
【问题描述】:

我已经开始使用 Google Colab 笔记本,但我仍然不知道如何更改笔记本的保存位置。我可以更改工作目录并将文件保存到我的 Google Drive 中的一个文件夹中,但实际的笔记本总是保存在一个名为 Colab Note 的文件夹中。这是我用来更改工作目录的代码。

from google.colab import drive
drive.mount('/content/drive', force_remount=True)

import os 

# Set your working directory to a folder in your Google Drive. This way, if your notebook times out,
# your files will be saved in your Google Drive!

# the base Google Drive directory
root_dir = "/content/drive/My Drive/"

# choose where you want your project files to be saved
project_folder = "Colab_Directory_Testing/"

def create_and_set_working_directory(project_folder):
  # check if your project folder exists. if not, it will be created.
  if os.path.isdir(root_dir + project_folder) == False:
    os.mkdir(root_dir + project_folder)
    print(root_dir + project_folder + ' did not exist but was created.')

  # change the OS to use your project folder as the working directory
  os.chdir(root_dir + project_folder)

  # create a test file to make sure it shows up in the right place
  !touch 'new_file_in_working_directory.txt'
  print('\nYour working directory was changed to ' + root_dir + project_folder + \
        "\n\nAn empty text file was created there. You can also run !pwd to confirm the current working directory." )

create_and_set_working_directory(project_folder)

但是当我保存笔记本时,它不会保存在工作目录中。任何想法如何让它保存在那里?

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案1】:

    默认情况下,Google Colab 将笔记本保存在 Google Drive 根目录中名为“Colab Notebooks”的文件夹中。如果要将笔记本保存到其他位置,可以使用以下步骤:

    首先,在您要保存笔记本的 Google Drive 中创建一个文件夹。为了这个例子,我们称它为“我的笔记本”。

    接下来,通过运行以下代码将您的 Google Drive 安装到 Colab 笔记本中:

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

    安装驱动器后,您可以通过运行以下代码将当前工作目录更改为要保存笔记本的文件夹:

    import os
    os.chdir('/content/drive/My Drive/My Notebooks')
    

    将工作目录更改为所需文件夹后,可以通过单击文件 > 保存或运行以下代码来保存当前笔记本:

    !jupyter notebook save
    

    这会将当前笔记本保存到您的 Google Drive 中的指定文件夹。

    请注意,jupyter notebook save 命令还将在与笔记本文件相同的目录中创建一个检查点文件 (.ipynb_checkpoints)。此检查点文件用于在笔记本崩溃或无响应时恢复您的笔记本。

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 2022-09-27
      • 2019-12-17
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多