Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a file with a newline using %%writefile -a command in jupyter?

Tags:

python

jupyter

I use a Jupyter Notebook to write a small program. Therefore, I want to write some cells into a .py file.

I use this magic %%writefile -a myfile.py. It works, but the problem is that the content is appended without a new line. So, I have to manually add a line break to myfile.py after running the cells.

How can I avoid this problem?

like image 446
xirururu Avatar asked Apr 09 '16 18:04

xirururu


1 Answers

Just put an empty line after %%writefile in the cell. For example:

In [1]: 
%%writefile -a myfile.py

a = 1

In [2]: 
%%writefile -a myfile.py

b = 2

Now:

In [2]: 
%less myfile.py

a = 1
b = 2

Add two empty lines to the beginning of a cell for an empty line between the exported code of two cells in the file myfile.py.

like image 101
Mike Müller Avatar answered Oct 11 '22 14:10

Mike Müller