LoginSignup
2
2

More than 5 years have passed since last update.

Python > subprocess > /dev/nullに出力する > Popen()の引数にstdout=sb.DEVNULL

Last updated at Posted at 2017-12-16
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)

数千ケースの処理を行う場合、それぞれのコマンド出力(stdout)は/dev/nullに設定したい。

subprocess.DEVNULL(原文)
Popen の stdin, stdout, stderr 引数に渡して、標準入出力を os.devnull から入出力するように指定するための特殊値です。

使ってみた。

code

test_subprocess_devnull_171216.py
import subprocess as sb
import time

cmd = "echo Are you listening me?"

# 1.
sb.Popen(cmd.split())
print("I'm all ears.")

time.sleep(1)  # msec
print("---")

# 2.
sb.Popen(cmd.split(), stdout=sb.DEVNULL)
print("I'm talking to myself.")

run
$ python3 test_subprocess_devnull_171216.py 
I'm all ears.
Are you listening me?
---
I'm talking to myself.

結果について

  • 1.のPopen()のstdoutは表示される [正常]
  • 2.のPopen()のstdoutは表示されない [正常]

'Are you listening me?'が'I'm all ears.'(しっかり聞いてますよ)の後に出力される。
これらのタイミングについては未消化。MIT(Most Important Task)が終わってから調べるかもしれない。

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