问题
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.
回答1:
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.
回答2:
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.
Example:
!ls
output: colabData/
%%writefile something.py
print("everything's fine.")
!ls
output: colabData/ something.py
%run something.py
output: everything's fine.
回答3:
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.
回答4:
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)
回答5:
I found it easier to edit the file locally.
- You can download it from the left panel.
- Right click on any file and download it.
- Next, edit the file.
- Next, upload the file.
- use
mvto move the file to the right location.
回答6:
Colab includes a text editor you can use to create, open, and delete .py files directly.
For example:
回答7:
There is an app called Python Compiler Editor that you can connect to your Google Drive account, edit files and save them back.
回答8:
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
Hope that helps.
来源:https://stackoverflow.com/questions/48687091/how-to-edit-and-save-text-files-py-in-google-colab