简体   繁体   中英

Running an interactive python script in C#

I've searched for a way to do this but have so far not been successful.

My question is similar to this one: How do I run a Python script from C#?

But in my case, the script goes more like this:

def main():
  i = raw_input("i = ")

I can redirect the output fine by creating a process like the following:

ProcessStartInfo info = new ProcessStartInfo("cmd", "/c python myFile.py");

How would I send input back to it?

Please note, the python script can be anything. I don't know when an input might be expected so using a streamwriter to write to the process by setting redirectStandardInput to true doesn't work..

Any help would be great. Thanks all!

You will have to redirect the stdin of your process. Here is a full example of how you can do that:

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx

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