注意点

Shellコマンド実行成功の場合

import subprocess

try :
    out = subprocess.check_output(['echo','aiueo'], stderr=subprocess.STDOUT)
    print('Success')
    print('STDOUT:' + out.decode('utf8'))

except subprocess.CalledProcessError as cerr:
    print('CalledProcessError occured')
    print('CMD:' + str(cerr.cmd))
    print('RET:' + str(cerr.returncode))
    print('STDOUT:' + cerr.output.decode('utf8'))
 
Success
STDOUT:aiueo

SHELLコマンド実行失敗の場合

import subprocess

try :
    out = subprocess.check_output(['cat','xxx'], stderr=subprocess.STDOUT)
    print('Success')
    print('STDOUT:' + out.decode('utf8'))

except subprocess.CalledProcessError as cerr:
    print('CalledProcessError occured')
    print('CMD:' + str(cerr.cmd))
    print('RET:' + str(cerr.returncode))
    print('STDOUT:' + cerr.output.decode('utf8'))
 
CalledProcessError occured
CMD:['cat', 'xxx']
RET:1
STDOUT:cat: xxx: No such file or directory

DOSコマンド実行成功の場合

import subprocess

try :
    out = subprocess.check_output('cmd /c echo aiueo', stderr=subprocess.STDOUT)
    print('Success')
    print('STDOUT:' + out.decode('ms932'))

except subprocess.CalledProcessError as cerr:
    print('CalledProcessError occured')
    print('CMD:' + cerr.cmd)
    print('RET:' + str(cerr.returncode))
    print('STDOUT:' + cerr.output.decode('ms932'))

 
Success
STDOUT:aiueo

DOSコマンド実行失敗の場合

import subprocess

try :
    out = subprocess.check_output('cmd /c dir a:', stderr=subprocess.STDOUT)
    print('Success')
    print('STDOUT:' + out.decode('ms932'))

except subprocess.CalledProcessError as cerr:
    print('CalledProcessError occured')
    print('CMD:' + cerr.cmd)
    print('RET:' + str(cerr.returncode))
    print('STDOUT:' + cerr.output.decode('ms932'))
 
CalledProcessError occured
CMD:cmd /c dir a:
RET:1
STDOUT:指定されたパスが見つかりません。

Python


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2012-10-24 (水) 01:48:51 (1508d)
ISBN10
ISBN13
9784061426061