pythonipythonipython-magic

what does '%%file test.py' mean in python?


Just as the title said. I'm trying to figure out what does the '%%' mean. It seems to be not a Placeholder here?

%%file test_theano.py
from theano import config
print 'using device:', config.device

Solution

  • That %%file command is not Python, but rather an IPython "magic" command. It puts the content which follows into a file named by its parameter.

    Some time ago it was renamed %%writefile, but the old name is still supported, though no longer documented. You can find the documentation for %%writefile here: https://ipython.org/ipython-doc/3/interactive/magics.html

    If you try it in an IPython shell it will be quite clear what it does - it creates a file called test_theano.py in the current directory.

    See also: How to load/edit/run/save text files (.py) into an IPython notebook cell?