Popen 和 Popen 调用时 Python 的奇怪行为写入标准输入

发布于 2024-11-15 23:27:25 字数 967 浏览 4 评论 0原文

这里有两个(相关的?)问题。

我试图编写一个程序来启动外部进程,然后同时从标准输出读取并写入标准输入。一切似乎都正常,但是该进程没有响应发送到其标准输入管道的数据。你知道为什么会这样吗?(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蔚蓝源自深海 2024-11-22 23:27:25

您所看到的是 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。尝试使用 python3python3.1 运行它。

What you're seeing is the expected behaviour of Python 2.x's input() function, which takes a line from sys.stdin (like raw_input()) and then evaluates it as Python code. It's generally a bad idea to use input() in Python 2.x :) In Python 3.x, input() was removed and raw_input() was renamed to input(), 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 with python3 or python3.1 instead.

浴红衣 2024-11-22 23:27:25

确保您实际上正在运行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 to raw_input() which became Python 3's input()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文