简体   繁体   中英

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

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?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM