jupyter notebook writefile magic: no end-of-line at the end

36 views
Skip to first unread message

John Cremona

unread,
May 1, 2018, 7:17:02 AM5/1/18
to Sage support
I was using the jupyter notebook's magic %%writefile, where you put "%%writefile [-a] filename.py" at the top of a cell and then evaluating the cell writes the cell's contents to the file (overwriting by default, or appending if you give the -a flag).

I found that the file so written to has the correct contents except that there is no final newline character.  This is particularly annoying when you try (as I did) writing the contents of several cells to the same file, using -a on all but the first, since after doing all that, you then have to edit the file to insert some newlines.

Putting a blank line at the bottom of the cell makes no difference, as it is ignored.  Putting a comment character # on the last line is no better since that gets prepended to the first line of the next cell whose contents is appended.

This is not Sage-specific since the same happens if I run jupyter-notebook from the command line (on ubuntu linux) and create a new python3 notebook. 

which suggests putting a blank line at the top of each appended cell, and that does work (though the resulting file still has no final newline).

This seems rather counter-intuitive to me!

John

slelievre

unread,
May 1, 2018, 10:11:39 PM5/1/18
to sage-support
Probably best asked on the Project Jupyter mailing list: jup...@googlegroups.com 
Reply all
Reply to author
Forward
0 new messages