1

I am trying to capture return (list) from python script below using C#. Stream reader gives me a null as a response. However, when I print the result I get the information in stream reader but when I use return(list) it does not.I have tried a lot of ways. Links and any info is appreciated. Python:

def main1():

    data1 =send_initdata("i", sock)

    list.append (send_data("a", sock,"1"))
    list.append (send_data("b", sock,"2"))

    #print (list)
    return (list)


if __name__ == '__main__':sys.exit(main1())  

C#:

    void run_cmd()
    {


        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"C:\Python27\python.exe";
        start.Arguments = string.Format("{0} {1}", "Function.Py", "");
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        start.CreateNoWindow = true;


        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                var result = reader.ReadLine();

            }
        }

1 Answer 1

3

The return from a command-line process is always an integer, and is intended to indicate the presence or absence of errors during execution. You can't return a list as you're trying to do here.

Having the script print the list contents to STDOUT (as your commented print statement does), and then read that as your C# code does is the correct way to go. Alternately the script could write the list to a file and then your C# code read that file.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for the information Andrew! Now in C# if I use scope = engine.ExecuteFile( fileName); How can I use scope to run python file? Python file which has:if name == 'main':sys.exit(main1()) Appreciate your help!
Sorry, I don't know much about the workings of Python, just command-line processes in general.
Thanks Andrew, By any chance you have any idea how can I capture single python function print command? var ipy = Python.CreateRuntime(options); dynamic Python_File = ipy.UseFile("Function.Py"); Python_File.main1();// how can I capture this output?
I don't know what Python.CreateRuntime is. Is that Iron Python? It's possible that the API just sends the output of the python script directly to the STDOUT of your C# process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.