10

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?

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
xirururu
  • 5,028
  • 9
  • 35
  • 64

1 Answers1

12

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.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161