2016-07-02 38 views
1

有沒有辦法將我最後一條ipython命令發送給腳本?ipython函數將最後一條命令發送給腳本

所需的使用會是這樣的:

$ tail script.py 
... 

[1] my_command = 0 
[2] %saveLastCmd script.py # equivalent to $ echo <command> >> script.py 

$ tail script.py 
.... 
my_command = 0 

[1] my_command = 1 
[2] %saveLastCmd script.py # equivalent to $ echo <command> >> script.py 

$ tail script.py 
.... 
my_command = 0 
my_command = 1 
+0

通過IPython的神奇功能,閱讀和打字我的問題到谷歌 – kilojoules

回答

2
# Save the contents of a cell or output to a .py file 

def foo(): print('bar') 
foo() 

# -f forces overwrite of output file, -a appends to file 

%save -f -a 'foo_in' _ih[-1] 
+0

你可以改變'-1'指數保存任何細胞,你想。使用'%save'foo'_ [ - 1]'也應該得到最後一個輸出。 – zhespelt

+0

我可以使用它來追加到我的示例中的退出腳本嗎? – kilojoules

+0

保存魔術幫助文檔的「%save」。 – zhespelt