Python script to Batch file

后端 未结 5 509
半阙折子戏
半阙折子戏 2021-01-03 10:26

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

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-03 10:58

    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%
    

提交回复
热议问题