如何在Google Colab中编辑和保存文本文件(.py)?

2024-06-01 07:19:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用!git clone https://github.com/llSourcell/Pokemon_GAN.git克隆了一个github repo。我想在Colab中修改一个.py文件。所以我使用了这里建议的%load filename.pyHow to load/edit/run/save text files (.py) into an IPython notebook cell?)。但是每当我运行这个命令时,我会在一段时间后断开连接。我想知道是否有其他的方法来编辑.py文件,而不必麻烦地下载到pc上,编辑然后重新上传。提前谢谢。


Tags: 文件pyhttpsgitgithubcom编辑clone
2条回答

不幸的是,colab似乎不支持%load行魔术(yet),但是,您可以使用!cat your_file.py查看文件内容,然后手动复制输出内容,将其写入新单元格,并在新单元格顶部写入%%writefile your_new_file_name.py以将其保存回实例。注意,这将不会保存到您的谷歌驱动器尚未。

Example:
!ls
output: colabData/

%%writefile something.py
print("everything's fine.")

!ls
output: colabData/ something.py

%run something.py
output: everything's fine.

我发现在本地编辑文件更容易。

  1. 您可以从左侧面板下载。
  2. 右键单击任何文件并下载它。
  3. 接下来,编辑文件。
  4. 接下来,上传文件。
  5. 使用mv将文件移动到正确的位置。

enter image description here

相关问题 更多 >