Blog categories

Comments

[Python] Subprocess 명령 결과 실시간으로 출력하기

[Python] Subprocess 명령 결과 실시간으로 출력하기

import subprocess
import sys
with open('test.log', 'w') as f:
    process = subprocess.Popen(your_command, stdout=subprocess.PIPE)
    # for python2, use ''
    for c in iter(lambda: process.stdout.read(1), ''): 
        sys.stdout.write(c)
        f.write(c)

    # for python3, use b''
    for c in iter(lambda: process.stdout.read(1), b''):
        sys.stdout.write(c)
        f.write(c)

or

import subprocess
import sys
with open('test.log', 'w') as f:
    process = subprocess.Popen(your_command, stdout=subprocess.PIPE)

    # for python2, use ''
    for line in iter(process.stdout.readline, ''):
        sys.stdout.write(c)
        f.write(c)

    # for python3, use b''
    for line in iter(process.stdout.readline, b''):
        sys.stdout.write(c)
        f.write(c)

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

div#stuning-header .dfd-stuning-header-bg-container {background-image: url(https://tech.sangron.com/wp-content/uploads/sites/2/2018/02/python_wallpaper_background.jpg);background-color: #3f3f3f;background-size: cover;background-position: top center;background-attachment: initial;background-repeat: no-repeat;}#stuning-header div.page-title-inner {min-height: 350px;}