Python script to Batch file

后端 未结 5 475
半阙折子戏
半阙折子戏 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:41

    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
    
    0 讨论(0)
  • 2021-01-03 10:44

    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.

    0 讨论(0)
  • 2021-01-03 10:48

    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.

    0 讨论(0)
  • 2021-01-03 10:49

    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.

    0 讨论(0)
  • 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%
    
    0 讨论(0)
提交回复
热议问题