1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2022-03-27

概要

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

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

方法

import subprocess
import sys


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}')
  • エラー出力もリアルタイムに出したいなら、stderr=subprocess.PIPEstderr=subprocess.STDOUT とするのが手軽。
asyncio版
import asyncio
import sys

async def run_shell(cmd):
    print(f'cmd: {cmd}')
    process = await asyncio.create_subprocess_shell(
        cmd,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.STDOUT,
    )

    async for line in process.stdout:
        sys.stdout.write(line.decode('UTF-8'))
    
    returncode = await process.wait()
    
    print(f'return: {returncode}')
    if returncode:
        raise Exception(f'Error! {cmd}')

# asyncio.run(run_shell('your-command-here'))
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?