4

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.

4
  • From your wording I'm guessing you are using Windows? Commented May 30, 2012 at 17:33
  • What do you mean with 'value'? If the return value of the program is enough, you can get it with %errorlevel% Commented May 30, 2012 at 17:34
  • Sorry, edited the question. I want to send an integer or string basically. Commented May 30, 2012 at 17:35
  • Looking at the big picture: in a broad sense, python and batch files both provide ways to automate tasks. Could the two scripts be merged into one? Commented May 30, 2012 at 17:47

5 Answers 5

18

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%
Sign up to request clarification or add additional context in comments.

4 Comments

yea, because it's natural to assume that someone is writing a batch file using bash on windows...
FUU guys, I'm trying to help OP here. From the original post it was not obvious, that this is a Windows question. I only saw that after I sent my answer.
Gave you an upvote for the latter half of your answer. The reason the guys are ribbing you is because it is obvious, just from the name of the question... they're not called batch files on *nix systems. ;)
This is exactly the answer I was looking for, that all the other answers failed to suggest. Thanks! I guess it still is cool to be able to get the exit value, but not nearly as powerful as what your answer provides.
6

If a int is enough for you, then you can use

sys.exit(value)

in your python script. That exits the application with a status code of value

In your batch file you can then read it as the %errorlevel% environment variable.

Comments

4

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.

1 Comment

By passing the desired value to sys.exit().
2

Ignacio is dead on. The only thing you can return is your exit status. What I've done previously is have the python script (or EXE in my case) output the next batch file to be run, then you can put in whatever values you'd like and run it. The batch file that calls the python script then calls the batch file you create.

Comments

1

You can try this batch script for this issue, as an example:

@echo off

REM     %1 - This is the parameter we pass with the desired return code for the Python script that will be captured by the ErrorLevel env. variable.  
REM     A value of 0 is the default exit code, meaning it has all gone well. A value greater than 0 implies an error
REM     and this value can be captured and used for any error control logic and handling within the script
   
set ERRORLEVEL=
set RETURN_CODE=%1

echo (Before Python script run) ERRORLEVEL VALUE IS: [ %ERRORLEVEL% ]
echo.

call python -c "import sys; exit_code = %RETURN_CODE%; print('(Inside python script now) Setting up exit code to ' + str(exit_code)); sys.exit(exit_code)"

echo.
echo (After Python script run) ERRORLEVEL VALUE IS: [ %ERRORLEVEL% ]
echo.

And when you run it a couple of times with different return code values you can see the expected behaviour:

PS C:\Scripts\ScriptTests> & '\TestPythonReturnCodes.cmd' 5

(Before Python script run) ERRORLEVEL VALUE IS: [ 0 ]

(Inside python script now) Setting up exit code to 5

(After Python script run) ERRORLEVEL VALUE IS: [ 5 ]

PS C:\Scripts\ScriptTests> & '\TestPythonReturnCodes.cmd' 3

(Before Python script run) ERRORLEVEL VALUE IS: [ 0 ]

(Inside python script now) Setting up exit code to 3

(After Python script run) ERRORLEVEL VALUE IS: [ 3 ]

PS C:\Scripts\ScriptTests

Comments

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.