简体   繁体   中英

Python's input() or raw_input() redirect to stderr instead of stdout

test = input("Test: ")

$ python3 test.py 2> /dev/null

I can input values but Test: does not show in the output. How do I specify to redirect input() to stdout?

First, you should save stdout

tempOut = sys.stdout

Then, you should redirect stdout in program

sys.stdout = sys.stderr

Next, you can read with "input"

data = input("Test: ")

In the end, you must redirect stdout to original

sys.stdout = tempOut

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM