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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48687091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:48:35  来源:igfitidea点击:

How to edit and save text files (.py) in Google Colab?

pythonjupyter-notebookgoogle-colaboratory

提问by randomName

I cloned a github repo using !git clone https://github.com/llSourcell/Pokemon_GAN.git. I wanted to modify a .py file inside Colab. So i used %load filename.pyas suggested here (How to load/edit/run/save text files (.py) into an IPython notebook cell?). But whenever i run this command, i get disconnected after some time. I was wondering if there is some other way to edit .py file without undergoing the hassle of downloading it to pc,editing and then re uploading it. Thanks in advance.

我使用 !git clone https://github.com/llSourcell/Pokemon_GAN.git. 我想在 Colab 中修改一个 .py 文件。所以我%load filename.py按照这里的建议使用(How to load/edit/run/save text files (.py) into an IPython notebook cell?)。但是每当我运行此命令时,我都会在一段时间后断开连接。我想知道是否有其他方法可以编辑 .py 文件,而无需经历将其下载到 PC、编辑然后重新上传的麻烦。提前致谢。

回答by rahul

You can use Ipython magic commands. Use below command

%pycat code.py

A pop up will appear displaying the code. You can copy it and edit it locally.
Remove the file using below command

!rm code.py

Copy the edited code to a cell in notebook and add below command at the top of the cell

%%writefile code.py

Run the cell. A file will be created with the contents present in the cell.

您可以使用 Ipython 魔术命令。使用下面的命令

%pycat code.py

会出现一个弹出窗口,显示代码。您可以复制它并在本地编辑它。
使用以下命令删除文件

!rm code.py

将编辑过的代码复制到 notebook 中的一个单元格中,并在单元格顶部添加以下命令

%%writefile code.py

运行单元格。将使用单元格中存在的内容创建一个文件。

回答by b.g.

Unfortunately, it seems, colab do not support %loadline magic (yet), and yet, you can see the file content using !cat your_file.pyand then manually, copy the output contents, write them to a new cell and write %%writefile your_new_file_name.pyat the top of the new cell to save this back to the instance. Note that, this will not be saved to your google drive yet.

不幸的是,colab 似乎不支持%loadline magic(还),但是,您可以使用!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.

回答by Bob Smith

Colab includes a text editor you can use to create, open, and delete .pyfiles directly.

Colab 包含一个文本编辑器,可用于直接创建、打开和删除.py文件。

For example:

例如:

enter image description here

在此处输入图片说明

回答by Netro

I found it easier to edit the file locally.

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

  1. You can download it from the left panel.
  2. Right click on any file and download it.
  3. Next, edit the file.
  4. Next, upload the file.
  5. use mvto move the file to the right location.
  1. 您可以从左侧面板下载它。
  2. 右键单击任何文件并下载它。
  3. 接下来,编辑文件。
  4. 接下来,上传文件。
  5. 用于mv将文件移动到正确的位置。

enter image description here

在此处输入图片说明

回答by Emmanuel Lopez

Solution:

解决方案:

p = """
Yadda yadda
whatever you want just don't use triple quotes.
"""

c = """text_file = open("text.text", "w+");text_file.write(p);text_file.close()""" 

exec(c)

回答by ??????? ????

Not a perfect solution but can be useful for someone.

不是一个完美的解决方案,但对某人有用。

You can use !cat file_name.pyto access file_name.pycontents, copy the contents in the next cell and now you can run it or edit it.

您可以使用 !cat file_name.py访问 file_name.py内容,复制下一个单元格中的内容,现在您可以运行或编辑它。

回答by Marafon Thiago

There is an app called Python Compiler Editorthat you can connect to your Google Drive account, edit files and save them back.

有一个名为Python Compiler Editor的应用程序,您可以连接到您的 Google Drive 帐户,编辑文件并将其保存回来。

回答by Sam

The easiest way is:

最简单的方法是:

1- Go to where you want the file to be with:

1- 转到您希望文件所在的位置:

%cd WhereYouWantItToBe

2- Then write using:

2-然后使用:

%%writefile NameOfFile.txt
Hey there here is the start of the text
and also here
here is the end

3- Now run this cell and the file is going to be saved where you decided in step one.

3- 现在运行此单元格,文件将保存在您在第一步中决定的位置。

回答by hossein hayati

you can edit it like this:

你可以像这样编辑它:

  1. click on the triple bar (≡ on the left side of your windows)
  2. click on files (the folder icon on the left)
  3. click on Mount Drive and mount your drive
  4. find your .py file and double click on it
  5. edit it
  6. press ctrl+s to save
  1. 单击三重栏(≡ 在窗口左侧)
  2. 单击文件(左侧的文件夹图标)
  3. 单击“安装驱动器”并安装驱动器
  4. 找到您的 .py 文件并双击它
  5. 编辑它
  6. 按 ctrl+s 保存

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

edit:
these steps were after cloning your code into your drive
you should first mount your drive and clone your repo into your drive

编辑:
这些步骤是在将您的代码克隆到您的驱动器之后,
您应该首先安装您的驱动器并将您的存储库克隆到您的驱动器中

回答by aneesh joshi

While I don't have a way of editing in the notebook, I will share my pipeline. Quite obvious really:

虽然我没有在笔记本中进行编辑的方法,但我将分享我的管道。真的很明显:

  • fork the repo or create a new one(for a new project)
  • create a branch just for uploading
  • make changes and push
  • evaluate
  • make changes
  • 分叉 repo 或创建一个新的(对于一个新项目)
  • 创建一个只用于上传的分支
  • 做出改变并推动
  • 评估
  • 做出改变

Hope that helps.

希望有帮助。