【问题标题】:iPython equivalent of Unix "sh" commandiPython 等效于 Unix“sh”命令
【发布时间】:2014-05-24 09:45:23
【问题描述】:

是否有等效于执行 Unix sh 命令的 iPython?我想通过具有一组行的 shell 文件执行程序的多次迭代,例如:

%run python_file.py data_file1
%run python_file.py data_file2
.
.
.
%run python_file.py data_fileN

python_file.py 有 sys 模块 argv 调用以允许命令行输入。

【问题讨论】:

  • 编写第二个执行它们的 pyhton 脚本。这是一个选择吗?
  • 如何编写一个程序,将可执行命令输出到 iPython shell?

标签: python shell unix ipython


【解决方案1】:

python_file.py 所在的目录中,创建一个名为run_python_file.py(或类似名称)的python 文件,它应该包含

from subprocess import call
from sys import argv 

if len(argv) <= 1:
    print "Usage..."

for i in range(0,int(argv[1])): #n has to be defined somewhere!
    call(['python','python_file.py','data_file'+str(i)])

在您的 iPython shell 中,您导航到 python_file.py 所在的文件夹,然后调用

%run run_python_file.py 5

如果你想用 0 到 4 的数据文件运行它

【讨论】:

  • 非常感谢您的快速回复!我真的很喜欢从子进程中使用“调用”方法的想法,但是当我运行代码时,我得到一个“WindowsError:[错误 2] 系统找不到指定的文件”。有什么解决办法吗?
  • 我想你可能会感兴趣:stackoverflow.com/questions/9531683/…
  • 很好,谢谢!当我添加参数“shell=True”时,它解决了 WindowsError 问题,但我没有让调用输出在命令行中执行。我尝试按照该链接中的建议添加“cmd”,但它只是使 Canopy 崩溃。
  • 我只是在 geany 编辑器中搜索完全不同的东西以使 linux 命令在 windows 中运行。答案也完全无关紧要。但最后这篇文章给了我一个想法,用一种不同的方法解决了我的问题。欢呼。为你的答案点赞。
猜你喜欢
  • 2011-03-25
  • 1970-01-01
  • 2011-12-26
  • 2021-12-16
  • 2011-07-12
  • 1970-01-01
  • 2021-12-18
  • 2014-08-05
  • 1970-01-01
相关资源
最近更新 更多