Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In IPython, how do you save and append to a file rather than overwriting it?

Tags:

python

ipython

In IPython, you can save parts of the current session by doing %save my_useful_session 10-20 23 to save lines 10-20 and line 23 to the file my_useful_session.py. If my_useful_session.py already exists, IPython promps you to overwrite the fail rather than append it. How can you append to an already existing file?

like image 968
Ryan Endacott Avatar asked Nov 24 '14 20:11

Ryan Endacott


People also ask

How do you save codes on IPython?

You can save your IPython session to a file with the %save command.

How do you append and read a file in Python?

Append and Read ('a+') : Open the file for reading and writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.

How do I save a file in Jupyter?

Saving a Jupter notebook Saving your edits is simple. There is a disk icon in the upper left of the Jupyter tool bar. Click the save icon and your notebook edits are saved.


2 Answers

I found the answer in this GitHub pull request. You can append to an existing file by passing the -a option to the save command. Note that the option must be passed before any of the other input. Ex: %save -a my_useful_session 10-20 23 will append lines 10-20 and 23 to the file my_useful_session.py.

like image 182
Ryan Endacott Avatar answered Nov 10 '22 01:11

Ryan Endacott


You can try with the following:

%save -a 
like image 43
Matt Avatar answered Nov 09 '22 23:11

Matt