簡體   English   中英

從IPython運行組合的Shell腳本(非交互式)

[英]Running combined shell script from IPython (non-interactively)

是否有可能以腳本非交互方式運行擴展的 IPython(可在交互式控制台中使用,請參見示例)? 我沒有在任何地方記錄它。

例:

#!/magical/ipython/command --that-i-want
from __future__ import print_function
d = !date +%s
!echo $d.s $(($d.s / 1000))

files = !ls -A
for i, f in enumerate(files):
    print("File number %s is '%s'" % (i, f))

預期輸出應為:(復制並粘貼到IPython交互式控制台的結果)

1463917269 1463917
File number 0 is '.ipynb_checkpoints'
File number 1 is 'file1.txt'
File number 2 is 'file2.txt'

當shebang是ipython它失敗並顯示:

d = !date +%s
    ^
SyntaxError: invalid syntax

您可以運行此文件。

vi shebang.py

get_ipython().run_cell("""
from __future__ import print_function
d = !date +%s
!echo $d.s $(($d.s / 1000))

files = !ls -A
for i, f in enumerate(files):
    print("File number %s is '%s'" % (i, f))
""")

ipython shebang.py

使用.ipy擴展名命名文件,然后IPython將能夠使用(大多數)語法擴展名運行該文件。

盡管IPython所做的只是語法糖,但是您可以在純Python中完成它。

(也請使用Python 3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM