1

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
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
yunlai xu
  • 13
  • 1
  • 4
  • `%%file` is an alias for the `%%writefile` cell magic in ipython, which writes the contents of this cell to the file specified. See: https://ipython.org/ipython-doc/3/interactive/magics.html – Craig Apr 02 '17 at 03:46

2 Answers2

12

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?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    Thanks @John Zwinck you are correct about it. I just realized it when I try it in an IPython notebook and get a file named test_theano.py. – yunlai xu Apr 02 '17 at 03:55
1

Just to give an example to support what John wrote.

Run this in Ipython (or some Ipython shell such as Jupyter):

%%file test.py

some_list=[1,2,3]

The program will return

Writing test.py

IPython created a test.py file in the imediate environment. Open the test.py and you would see:

 some_list=[1,2,3]
maleckicoa
  • 481
  • 5
  • 8