0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#python でサブプロセスを起動し #shell に引数・環境変数・標準入力を与え標準出力する例

Last updated at Posted at 2019-04-12
  • subprocess.run で色々できる
  • .stdout で標準出力結果を得られる

shell

#!/usr/bin/env bash

echo "Stdin is" $(cat /dev/stdin)
echo SOME_ENV is "$SOME_ENV"

echo First arg is "$1"

echo "Stdout!"

python

#!/usr/bin/env python3

import subprocess

stdout_of_shell = subprocess.run(['./eg.sh', 'banana boat'], \
  stdout=subprocess.PIPE, \
   input='Water fall!', \
   env={"SOME_ENV":'Wow'}, \
   encoding='utf-8').stdout

print(stdout_of_shell)

exe

$ ./proc.py
Stdin is Water fall!
SOME_ENV is Wow
First arg is banana boat
Stdout!

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?