Use python return parameter to command line

16,572

You don't say what environment you are using (*nix/Windows/OSX etc), but for *nix and shell scripts you can do

# Python
# whatever.py
import sys
sys.stdout.write('abc')
sys.exit(0)

# In your shell
OUT=`python whatever.py`
echo $OUT
# Will print abc, and it's stored in the variable `OUT` for later consumption.

EDIT (for Windows):

# Python
# whatever.py
import sys
sys.stdout.write('abc')
sys.exit(0)

# In a .bat file, or cli.
python whatever.py > temp.txt
set /p OUT=<temp.txt
# Creates/replaces a file called temp.txt containing the output of whatever.py
# then sets the `OUT` var with the contents of it.

The Windows way of doing it is not as nice and neat as the *nix way unfortunately.

Share:
16,572
Jimmy Lin
Author by

Jimmy Lin

Updated on November 22, 2022

Comments

  • Jimmy Lin
    Jimmy Lin over 1 year

    I want to build a connection between batch file and python program.

    I want to use python to get a parameter which is "abc", and let the batch file use the parameter "abc" to do other thing.

    How can I return a parameter to command line in python ?

    Thanks for your help.