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

后端 未结 11 1804
[愿得一人]
[愿得一人] 2020-12-24 01:16

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 fil

相关标签:
11条回答
  • 2020-12-24 01:38

    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

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

    0 讨论(0)
  • 2020-12-24 01:39

    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.

    Updates: Now there are lot more easy and convenient options.

    1. In the files section, there is an option to upload files or you can double click on the file, make changes and ctrl+s to save those changes.
    2. You can also use https://github.com/abhishekkrthakur/colabcode to edit using visual studio code server.
    0 讨论(0)
  • 2020-12-24 01:42

    The easiest way is:

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

    %cd WhereYouWantItToBe
    

    2- Then write using:

    %%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.

    0 讨论(0)
  • 2020-12-24 01:42

    Easiest solution just double click on the file you want to edit.The file opens and edit the file,save it and that's that.You are done

    0 讨论(0)
  • 2020-12-24 01:52

    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.
    
    0 讨论(0)
  • 2020-12-24 01:55

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

    All is done in the Files view (see below).

    • To create or delete a file, right click and choose "New file" or "Delete file".
    • To edit a file, double click on it. It appears on the right portion of your screen. Make any changes you wish. Changes are saved automatically.

    0 讨论(0)
提交回复
热议问题