Python-子进程和python shell [英] Python - subprocesses and the python shell

查看:121
本文介绍了Python-子进程和python shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将外壳扩展到非python子进程,并允许其从python继承stdin和stdout. -我正在使用subprocess.Popen

I am trying to shell out to a non-python subprocess and allow it to inherit the stdin and stdout from python. - i am using subprocess.Popen

如果我是从控制台调用的,这可能会起作用,但是当我使用python shell时,它肯定不起作用

This would probably work if I am calling from a console, but it definitely doesn't work when I am using the python shell

(顺便说一句,我正在使用IDLE)

(I am using IDLE by the way)

有没有办法说服python允许非python子进程将其stdout打印到python shell?

Is there any way to convince python to allow a non python subprocess to print it's stdout to the python shell?

推荐答案

这既适用于脚本,也适用于交互式解释器,但不适用于IDLE:

This works both from a script and from the interactive interpreter, but not from IDLE:

subprocess.Popen(whatever, stdin=sys.stdout, stdout=sys.stdin)

您不能将IDLE分配给sys.stdinsys.stdout的对象用作subprocess.Popen的参数.这些对象(IDLE Shell窗口的接口)类似于文件,但是它们不是具有fileno属性的真实文件句柄,并且类似Unix的操作系统要求将Fileno指定为子进程的stdin或stdout. .我不能说Windows,但是我想它也有类似的要求.

You can't use the objects which IDLE assigns to sys.stdin and sys.stdout as arguments to subprocess.Popen. These objects (the interfaces to the IDLE shell window) are file-like, but they're not real file handles with fileno attributes, and Unix-like operating systems require a fileno to be specified as the stdin or stdout for a subprocess. I cannot speak for Windows, but I imagine it has similar requirements.

这篇关于Python-子进程和python shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆