IPython%保存魔术错误

时间:2013-06-11 00:35:09

标签: ipython ipython-notebook

我正在尝试使用%loadpy魔法保存我加载到我的IPython笔记本中的python文件。当我尝试使用%save settings.py保存文件时出现以下错误:

File `settings.py` exists. Use `%save -f 'settings.py' -f` to force overwrite

然后,当我使用%save -f 'settings.py' -f时,我收到错误:

'-f' was not found in history, as a file, url, nor in the user namespace.

%save -f 'settings.py'也会出现错误'' was not found in history, as a file, url, nor in the user namespace.

知道如何正确保存.py文件以覆盖以前的版本吗?谢谢!

2 个答案:

答案 0 :(得分:8)

In [13]: %save -f settings.py 1-10 # saves lines 1 to 10 to settings.py
In [14]: %save? # Gives you the help on the save command


Usage:
  %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...

n1-n2,n3-n4是您要保存的行的范围。 n5,n6是您要保存的单个行号。添加-f选项以强制保存。

答案 1 :(得分:-1)

要回答具体问题,可以使用标记-f强制覆盖文件,如%save帮助所示:

-f: force overwrite. If file exists, %save will prompt for overwrite unless -f is given.

与IPython 线魔法相关的单%。在这种情况下,只保存单元格的某些行。任何行的组合都可以保存。

如果您希望将整个单元格保存到文件,则可以使用%%writefile IPython magic命令。请注意,%%writefile有两个百分号%%,这意味着该命令是一个 cell magic 命令,适用于整个单元格。