Python script to Batch file


Question

I have a batch file that runs a python script. I am running Python 3.2. I want to send a variable like an integer or string from the python script back to the batch file, is this possible?

I know I can accept command line arguments in the Python script with sys.argv. Was hoping there was some feature that allows me to do the reverse.

1
1
7/20/2015 10:02:03 PM

Accepted Answer

You can't "send" a string. You can print it out and have the calling process capture it, but you can only directly return numbers from 0 through 255.

3
5/30/2012 5:37:04 PM

In your Python script, just write to standard out: sys.stdout.write(...)

I'm not sure what scripting language you are using, maybe you could elaborate on that, for now I'll assume you are using bash (unix shell). So, In your batch script you can have the output of the python script into a variable like this:

#run the script and store the output into $val
val = `python your_python_script.py`
#print $val
echo $val

EDIT it turns out, it is Windows batch

python your_python_script.py > tmpFile 
set /p myvar= < tmpFile 
del tmpFile 
echo %myvar%

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon