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
How to edit and save text files (.py) in Google Colab?
提问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.py
as 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 %load
line magic (yet), and yet, you can see the file content using !cat your_file.py
and then manually, copy the output contents, write them to a new cell and write %%writefile your_new_file_name.py
at 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 似乎不支持%load
line 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
回答by Netro
回答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.py
to access file_name.py
contents, 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:
你可以像这样编辑它:
- click on the triple bar (≡ on the left side of your windows)
- click on files (the folder icon on the left)
- click on Mount Drive and mount your drive
- find your .py file and double click on it
- edit it
- press ctrl+s to save
- 单击三重栏(≡ 在窗口左侧)
- 单击文件(左侧的文件夹图标)
- 单击“安装驱动器”并安装驱动器
- 找到您的 .py 文件并双击它
- 编辑它
- 按 ctrl+s 保存
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.
希望有帮助。