Created
August 8, 2013 09:28
-
-
Save mdjhny/6183147 to your computer and use it in GitHub Desktop.
管道和输入同时作为输入的解决办法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
如果脚本从管道获取输入,同时需要有输入交互,可以采取以下三种措施: | |
http://mail.python.org/pipermail/python-list/2000-March/020133.html | |
* Put the data in a file and pass the name of the file to your | |
script, then open that file: | |
lines = open(sys.argv[1]).readlines() | |
This is probably the nicest solution, and the least prone to making | |
wierd things happen if your script gets run differently. | |
* Open /dev/tty to replace sys.stdin: | |
sys.stdin = open('/dev/tty') | |
a = raw_input('Prompt: ') | |
* Redirect stdin to another file handle when you run your script, and | |
read from that: | |
sys.stdin = os.fdopen(3) | |
a = raw_input('Prompt: ') | |
$ (echo -n test | ./x.py) 3<&0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment