通过管道输入到脚本,然后从用户获取输入
假设我想将输入通过管道传输到 Python 程序,然后在命令行上从用户那里获取输入。
echo http://example.com/image.jpg | python solve_captcha.py
solve_captcha.py
的内容为:
import sys
image_url = sys.stdin.readline()
# Download and open the captcha...
captcha = raw_input("Solve this captcha:")
# do some processing...
上面将触发 EOFError: EOF when read a line
错误。
我还尝试添加 sys.stdin.close()
行,这会提示 ValueError: I/O 操作已关闭的文件
。
您可以将信息通过管道传输到 stdin,然后从用户那里获取输入吗?
注意:这是一个精简的、简化的示例 - 请不要回答说“为什么你想在第一种情况下这样做”,这真的很令人沮丧。我只是想知道是否可以将信息通过管道传输到 stdin,然后提示用户输入。
Let's say I want to pipe input to a Python program, and then later get input from the user, on the command line.
echo http://example.com/image.jpg | python solve_captcha.py
and the contents of solve_captcha.py
are:
import sys
image_url = sys.stdin.readline()
# Download and open the captcha...
captcha = raw_input("Solve this captcha:")
# do some processing...
The above will trigger a EOFError: EOF when reading a line
error.
I also tried adding a sys.stdin.close()
line, which prompted a ValueError: I/O operation on closed file
.
Can you pipe information to stdin
and then later get input from the user?
Note: This is a stripped down, simplified example - please don't respond by saying "why do you want to do that in the first case," it's really frustrating. I just want to know whether you can pipe information to stdin
and then later prompt the user for input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是为了模拟
raw_input()
,因为我遇到了和你一样的问题。整个stdin
和clear
丑陋只是为了让它看起来更漂亮。这样您就可以看到您正在输入的内容。Made this up to emulate
raw_input()
, since I had the same problem as you. The wholestdin
andclear
ugliness is simply to make it look pretty. So that you can see what you are typing.我更新了@Bob的答案以支持删除、ctrl + [左、右、home、end]按键,并简化了标准输出清除和重写。
I updated @Bob's answer to support delete, ctrl + [left, right, home, end] keypresses and simplified the stdout clearing and rewriting.
这个问题没有通用的解决方案。最好的资源似乎是此邮件列表线程。
基本上,通过管道连接到程序中会将程序的 stdin 连接到该管道,而不是终端。
邮件列表线程有几个相对简单的解决方案对于 *nix:
打开 /dev/tty 以替换 sys.stdin:
在运行脚本时将 stdin 重定向到另一个文件句柄,并从中读取:
以及<一href="http://mail.python.org/pipermail/python-list/2000-March/055320.html" rel="noreferrer">使用诅咒的建议。请注意,邮件列表线程古老,因此您可能需要修改您选择的解决方案。
There isn't a general solution to this problem. The best resource seems to be this mailing list thread.
Basically, piping into a program connects the program's
stdin
to that pipe, rather than to the terminal.The mailing list thread has a couple of relatively simple solutions for *nix:
Open /dev/tty to replace sys.stdin:
Redirect stdin to another file handle when you run your script, and read from that:
as well as the suggestion to use curses. Note that the mailing list thread is ancient so you may need to modify the solution you pick.
bash 有进程替换,它创建了一个 FIFO,您可以将其视为文件,因此
您可以使用
您将把solve_capcha.py 作为文件打开的第一个参数,并且我认为 sys.stdin 仍然可以读取从键盘输入。
编辑:如果您不使用 bash,则可以使用 mkfifo 在任何 POSIX 系统上完成相同的操作:
FIFO 将阻塞(等待而不关闭)输出。
bash has process substitution, which creates a FIFO, which you can treat like a file, so instead of
you can use
You would open first argument to solve_capcha.py as a file, and I think that sys.stdin would still be available to read input from the keyboard.
Edit: if you're not using bash, you can use
mkfifo
to accomplish the same thing on any POSIX system:The FIFO will block (wait without closing) for output.
您可以关闭标准输入,然后重新打开它以读取用户输入。
You can close stdin and then reopen it to read user input.