LoginSignup
1
2

More than 1 year has passed since last update.

シェルコマンドをPythonから実行して結果をリアルタイムに標準出力に出す方法

Last updated at Posted at 2022-03-27

概要

Python の subprocess を使うことでシェルコマンドを実行可能だが、実行結果をリアルタイムに標準出力へとパイプする方法(かつエラーハンドリングが正しく行われる方法)がわかりづらいのでメモしておく。

ちなみに subprocess.run ではリアルタイムなパイプはできなかった。

方法

import subprocess
import sys
import tempfile


def run_shell(cmd):
    print(f'cmd: {cmd}')
    with subprocess.Popen(cmd, encoding='UTF-8', stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
        for line in p.stdout:
            sys.stdout.write(line)
        p.wait()
        for line in p.stderr:
            sys.stdout.write(line)
        print(f'return: {p.returncode}')
        if p.returncode:
            raise Exception(f'Error! {cmd}')
1
2
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
1
2