开发者

Pipe output of a command to an interactive python session?

What I'd like to do is something like

 $echo $PATH | python --remain-interactive "x = raw_input().split(':')"
 >>>
 >>> print x
 ['/usr/local/bin', '/usr/bin', '/bin']

I suppose ipython solution would be best. If this isn't achievable, what would be your solution开发者_开发百科 for the situation where I want to process output from various other commands? I've used subprocess before to do it when I was desperate, but it is not ideal.

UPDATE: So this is getting closer to the end result:

 echo $PATH > /tmp/stdout.txt; ipython -i -c 'stdout = open("/tmp/stdout.txt").read()'

Now how can we go about bending this into a form

 echo $PATH | pyout

where pyout is the "magic solution to all my problems". It could be a shell script that writes the piped output and then runs the ipython. Everything done fails for the same reasons bp says.


In IPython you can do this

x = !echo $$$$PATH

The double escape of $ is a pain though

You could do this I guess

PATH="$PATH"
x = !echo $PATH
x[0].split(":")


The --remain-interactive switch you are looking for is -i. You also can use the -c switch to specify the command to execute, such as __import__("sys").stdin.read().split(":"). So what you would try is: (do not forget about escaping strings!)

echo $PATH | python -i -c x = __import__(\"sys\").stdin.read().split(\":\")

However, this is all that will be displayed:

>>>

So why doesn't it work? Because you are piping. The python intepreter is trying to interactively read commands from the same sys.stdin you are reading arguments from. Since echo is done executing, sys.stdin is closed and no further input can happen.

For the same reason, something like:

echo $PATH > spam
python -i -c x = __import__(\"sys\").stdin.read().split(\":\") < spam

...will fail.

What I would do is:

echo $PATH > spam.bar
python -i my_app.py spam.bar

After all, open("spam.bar") is a file object just like sys.stdin is :)


Due to the Python axiom of "There should be one - and preferably only one - obvious way to do it" I'm reasonably sure that there won't be a better way to interact with other processes than the subprocess module.

It might help if you could say why something like the following "is not ideal":

>>> process = subprocess.Popen(['cmd', '/c', 'echo %PATH%'], stdout=subprocess.PIPE)
>>> print process.communicate()[0].split(';')

(In your specific example you could use os.environ but I realise that's not really what you're asking.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜