Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Add option append (-a) to %save

Add newline after every write to make sure successive appends work.
  • Loading branch information...
commit c0dc59939f4bce81d15e51c4456a1707605b1273 1 parent 6ce7c7a
Dominik Dabrowski authored
Showing with 17 additions and 5 deletions.
  1. +17 −5 IPython/core/magics/
22 IPython/core/magics/
@@ -61,6 +61,9 @@ def save(self, parameter_s=''):
-f: force overwrite. If file exists, %save will prompt for overwrite
unless -f is given.
+ -a: append to file. Open file in append mode and end every write
+ on a newline.
This function uses the same syntax as %history for input ranges,
then saves the lines to the filename you specify.
@@ -70,14 +73,16 @@ def save(self, parameter_s=''):
If `-r` option is used, the default extension is `.ipy`.
- opts,args = self.parse_options(parameter_s,'fr',mode='list')
+ opts,args = self.parse_options(parameter_s,'fra',mode='list')
raw = 'r' in opts
force = 'f' in opts
+ append = 'a' in opts
+ mode = 'a' if append else 'w'
ext = u'.ipy' if raw else u'.py'
fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
if not fname.endswith((u'.py',u'.ipy')):
fname += ext
- if os.path.isfile(fname) and not force:
+ if os.path.isfile(fname) and not force and not append:
overwrite ='File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
except StdinNotImplementedError:
@@ -91,9 +96,16 @@ def save(self, parameter_s=''):
except (TypeError, ValueError) as e:
print e.args[0]
- with,'w', encoding="utf-8") as f:
- f.write(u"# coding: utf-8\n")
- f.write(py3compat.cast_unicode(cmds))
+ out = py3compat.cast_unicode(cmds)
+ with, mode, encoding="utf-8") as f:
+ if append:
+ f.write(out)
+ # make sure we end on a newline
+ if not out.endswith(u'\n'):
+ f.write(u'\n')
+ else:
+ f.write(u"# coding: utf-8\n")
+ f.write(out)
print 'The following commands were written to file `%s`:' % fname
print cmds
Please sign in to comment.
Something went wrong with that request. Please try again.