LoginSignup
5
6

More than 5 years have passed since last update.

bat から Python スクリプトのリターンコードを取得する

Last updated at Posted at 2012-02-22

なんかタイトルが釣りっぽいけど推敲してる時間もないので勘弁して。
例えばこんな Python コードがあるとする。

hoge.py
# encoding: utf-8
import sys

if __name__=='__main__':
    if なんとかかんとか:
        hoge1()
        sys.exit(0)
    else:
        sys.exit(1)

これをバッチファイルから呼び出したあと、%ERRORLEVEL% sys.exit() の値が取得できる。

hoge.bat
@echo off
python3 hoge.py
IF %ERRORLEVEL%==0 foo.bat

これで sys.exit() の値が 0 なら foo.bat を実行して、それ以外なら何もしない的なことができる。

ただ %ERRORLEVEL% って 128 くらいまではシステムで役割が決まってるって話をどっかで見たのでやっちゃいけないかも知れんです。
とある処理をしたくて Python スプリクトの結果をなんとか受け取れないか四苦八苦した結果がコレなんですが、該当のコードはまた後ほど。

※まさかバッチファイルまでシンタックスハイライトしてくれるとは...。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6