Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

Thanks for the help!

share|improve this question
From your wording I'm guessing you are using Windows? – Al G May 30 '12 at 17:33
Yes, Windows XP. – dawnoflife May 30 '12 at 17:33
What do you mean with 'value'? If the return value of the program is enough, you can get it with %errorlevel% – mata May 30 '12 at 17:34
Sorry, edited the question. I want to send an integer or string basically. – dawnoflife May 30 '12 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? – abought May 30 '12 at 17:47

4 Answers

up vote 3 down vote accepted

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.

share|improve this answer
1  
How can I return numbers? – dawnoflife May 30 '12 at 17:38
1  
sys.exit(number) – mayhewr May 30 '12 at 17:39
1  
By passing the desired value to sys.exit(). – Ignacio Vazquez-Abrams May 30 '12 at 17:39

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.

share|improve this answer

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%
share|improve this answer
2  
yea, because it's natural to assume that someone is writing a batch file using bash on windows... – mata May 30 '12 at 17:42
1  
Batch files. As in cmd.exe. – Ignacio Vazquez-Abrams May 30 '12 at 17:42
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. – bpgergo May 30 '12 at 17:45
1  
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. ;) – Chris Krycho Jun 8 '12 at 19:38
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. – leetNightshade Sep 5 '12 at 22:29

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.