cancel
Showing results for 
Search instead for 
Did you mean: 

Python Scripts with Input and Output

NikhilSathe
Level 4
Hi
I want to execute a Python script and take back the output it gives (may be text).
Earlier I downloaded the latest Environment utility having action "Start Process Read Stderr ad Stdout".
When I execute the script using the "Start Process" action it executes, but it is not able to accept any response.
The new action is giving me error "Internal : Could not execute code stage because exception thrown by code stage: The specified executable is not a valid application for this OS platform."
I am having 64 bit Windows 10 laptop.

Python code:
import sys
sys.stdout.write('Returning from Python\n')
sys.stdout.flush()
sys.exit(0)

Is there any other way to it? I know writing to a file is an option, but I am taking it as a last resort.


------------------------------
Nikhil Sathe
Sr Consultant
Capgemini Technology Services
------------------------------
3 REPLIES 3

MandarBrahme
Level 2
Hi Nikhil

There is a bit of twist but you could run this python script through CMD (Dos Prompt) and in the start process create a batch file to execute this script. In a way if you see it would be a part of your BOT but still would be outside of your BOT operation and would work exactly as a silos operation.

Please let me know if this helps 🙂

Best Regards
Mandar Brahme

------------------------------
Mandar Brahme
Infrastructure Manager
Cognizant Technology Solutions
Asia/Kolkata
------------------------------

ShubhamJangam
Level 3
Hi Nikhil 

Please Check Input parameter for the action:
Arguments: Python file path
Process Name: Python Exe path

Regards,
Shubham Jangam

------------------------------
Shubham Jangam
RPA Developer
Webonise Lab
Asia/Kolkata
------------------------------

Hi All
Thanks for the suggestions.
I wrote a small C# script using Process.start, which takes inputs as Python interpreter, Python script to execute and then arguments if any.
The Python script also need to be written in a supporting way to have stdout and stderr. 
C# code :
// e.g. start.FileName = "C:\\Python27\\python.exe";
// Argument 0 = Path to your python script (example : "C:\\add_them.py")
// Argument 1 = first arguement taken from  C#'s main method's args variable
// Argument 2 = second argument.... and so on...
//
// pass these to your Arguments property of your ProcessStartInfo instance
// In the Python script sys.argv[0] will contain the script name.
// ******************************************************
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = Python_Executable_Path;
start.Arguments = Python_Script_Path + " " + Python_Arguments;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
          using (StreamReader reader = process.StandardOutput,
                                         reader_err = process.StandardError )
    {
          string error = reader_err.ReadToEnd();
          Error_Output = error;
          string result = reader.ReadToEnd();
          JSON_Output = result;   
     }
}

Sample Python :
## Write Print/stdout.write statements only  when necessary
## Write Print/stdout.write statements only  when necessary
## Avoid intermediate prints used for debugging in final version
## Otherwise it will be captured as output along with JSON
## sys.argv[0] contains script name
## import sys and json lib
/* ================ Sample Python Script ====================
import sys
import json
cnt = len(sys.argv) - 1
#print("Arguments received: {}".format(cnt)) ==>> AVOID THIS!!
sum1 = 0
if (cnt < 1 😞   
    sys.stderr.write("No arguments were passed..!!")
    sys.stderr.flush()
    sys.exit(0)
else:
    j = 1
    for i in sys.argv:
        if (j > 1):   # skip the first argument
            sum1 = sum1 + int(i)
            j = j + 1
            outdict = {'Name':'John', 'Grade':'Consultant', 'Param cnt':cnt, 'Points':sum1}
            json_string = json.dumps(outdict)
sys.stdout.write(json_string)
sys.stdout.flush()
sys.exit(0)
====================================================  */

------------------------------
Nikhil Sathe
Sr Consultant
Capgemini Technology Services
------------------------------