Popen 和 Popen 调用时 Python 的奇怪行为写入标准输入
这里有两个(相关的?)问题。
我试图编写一个程序来启动外部进程,然后同时从标准输出读取并写入标准输入。一切似乎都正常,但是该进程没有响应发送到其标准输入管道的数据。你知道为什么会这样吗?(1)
现在第二个问题已经解决了。
我编写了两个测试脚本,第一个是这样的:
# recv.py
while True:
print(input())
第二个旨在使用 Popen 调用另一个,给定它是一些任意输入:
# send.py
recv = subprocess.Popen(["python", "recv.py"], stdin=subprocess.PIPE)
recv.stdin.write(b"Hello\n")
recv.stdin.write(b"World.\n")
这是我运行它时得到的:
skyler@pro:testing$ python send.py
skyler@pro:testing$ Traceback (most recent call last):
File "recv.py", line 30, in <module>
main()
File "recv.py", line 26, in main
print(input())
File "<string>", line 1, in <module>
NameError: name 'Hello' is not defined
无论出于何种原因,看起来 input()
的结果都被视为行的一部分,而不是字符串,确实在我在recv.py中设置了一个变量Hello
,它打印了Hello
的内容。为什么会发生这种情况?(2)
我在 Mac OSX 上运行 python 3.1.2。
Two (related?) questions here.
I was trying to write a program to start an external process, and then simultaniouly read from stdout and write to stdin. Everything seemed to be working, however the process was not responding to the data sent to its stdin pipe. Do you know why this would be?(1)
This second question is solved now.
I wrote two testing scripts, the first was as such:
# recv.py
while True:
print(input())
The second was designed to call the other using Popen, the give it some arbitrary input:
# send.py
recv = subprocess.Popen(["python", "recv.py"], stdin=subprocess.PIPE)
recv.stdin.write(b"Hello\n")
recv.stdin.write(b"World.\n")
This is what I got when I ran it:
skyler@pro:testing$ python send.py
skyler@pro:testing$ Traceback (most recent call last):
File "recv.py", line 30, in <module>
main()
File "recv.py", line 26, in main
print(input())
File "<string>", line 1, in <module>
NameError: name 'Hello' is not defined
It looks like for whatever reason the result of input()
is being treated like part of the line, instead of a string, indeed when I set a variable Hello
in recv.py, it printed the contents of Hello
. Why is this happening?(2)
I'm running python 3.1.2 on Mac OSX.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所看到的是 Python 2.x 的
input()
函数的预期行为,该函数从sys.stdin
获取一行(例如raw_input()< /code>),然后将其评估为 Python 代码。在 Python 2.x 中使用
input()
通常是一个坏主意:) 在 Python 3.x 中,input()
被删除,并且raw_input()< /code> 已重命名为
input()
,这可能就是您对其功能感到困惑的原因。尽管您可能已经安装了 Python 3.x,但您并未执行它。
python
命令可能(希望如此!)仍然是系统安装的 Python 2.x。尝试使用python3
或python3.1
运行它。What you're seeing is the expected behaviour of Python 2.x's
input()
function, which takes a line fromsys.stdin
(likeraw_input()
) and then evaluates it as Python code. It's generally a bad idea to useinput()
in Python 2.x :) In Python 3.x,input()
was removed andraw_input()
was renamed toinput()
, which may be why you're confused about what it does.You're not executing Python 3.x, even though you may have it installed. The
python
command is probably (hopefully!) still the system-installed Python 2.x. Try running it withpython3
orpython3.1
instead.确保您实际上正在运行Python 3?这看起来很像 Python 2.x
input()
行为,它将输入解释为 Python 表达式(而不是raw_input()
,它成为 Python 3 的>输入())。
Make sure that you're actually running Python 3? That looks suspiciously like the Python 2.x
input()
behavior, where it would interpret the input as Python expressions (as opposed toraw_input()
which became Python 3'sinput()
).