【问题标题】:what does '%%file test.py' mean in python?python中的'%%file test.py'是什么意思?
【发布时间】:2017-08-27 02:50:56
【问题描述】:

正如标题所说。我试图弄清楚“%%”是什么意思。这里好像不是占位符?

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

【问题讨论】:

标签: python ipython ipython-magic


【解决方案1】:

只是举一个例子来支持约翰写的东西。

在 Ipython(或一些 Ipython shell,如 Jupyter)中运行:

%%file test.py

some_list=[1,2,3]

程序将返回

编写 test.py

IPython 在即时环境中创建了一个 test.py 文件。 打开 test.py 你会看到:

 some_list=[1,2,3]

【讨论】:

    【解决方案2】:

    %%file 命令不是 Python,而是 IPython 的“魔法”命令。它将后面的内容放入由其参数命名的文件中。

    前段时间它被重命名为%%writefile,但仍然支持旧名称,尽管不再记录。您可以在此处找到%%writefile 的文档:https://ipython.org/ipython-doc/3/interactive/magics.html

    如果您在 IPython shell 中尝试它,它将非常清楚它的作用 - 它会在当前目录中创建一个名为 test_theano.py 的文件。

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

    【讨论】:

    • 谢谢@John Zwinck,你是对的。当我在 IPython notebook 中尝试并得到一个名为 test_theano.py 的文件时,我才意识到这一点。
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2012-01-23
    相关资源
    最近更新 更多