izac89
izac89

Reputation: 3930

Writing to stdin within the program?

Is there a way of doing something like sys.stdin.write("text") inside the program, to simulate a text being enter from a user, instead of using input() or equivalent and wait for the user to type something?

I am asking because I have a bug in my program that I am unable to fix, the bug was posted yesterday yet no answers got till now, and this is my last resort.

(Windows XP, Python3.3)

Thanks.

Upvotes: 0

Views: 119

Answers (1)

Fred Foo
Fred Foo

Reputation: 363527

There are various hacks to accomplish this, e.g.

from io import StringIO
import sys

sys.stdin = StringIO("user says hello!")

This should work across modules. Similarly, you can replace sys.stdin with a stream attached to a series of canned user inputs.

Upvotes: 1

Related Questions