programing tip

Python 함수 호출에서 stdout 출력을 캡처하는 방법은 무엇입니까?

itbloger 2020. 9. 9. 07:46
반응형

Python 함수 호출에서 stdout 출력을 캡처하는 방법은 무엇입니까?


객체에 뭔가를하는 Python 라이브러리를 사용하고 있습니다.

do_something(my_object)

변경합니다. 그렇게하는 동안 몇 가지 통계를 stdout에 인쇄하고이 정보를 파악하고 싶습니다. 적절한 해결책은 do_something()관련 정보를 반환 하도록 변경 하는 것입니다 .

out = do_something(my_object)

하지만 개발자 do_something()가이 문제를 해결하기까지는 시간이 좀 걸릴 것입니다. 해결 방법 do_something()으로 stdout에 쓰는 내용을 구문 분석하는 것에 대해 생각했습니다 .

코드의 두 지점 사이에서 stdout 출력을 캡처하려면 어떻게해야합니까?

start_capturing()
do_something(my_object)
out = end_capturing()

?


이 컨텍스트 관리자를 사용해보십시오.

# import StringIO for Python 2 or 3
try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

import sys

class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self
    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio    # free up some memory
        sys.stdout = self._stdout

용법:

with Capturing() as output:
    do_something(my_object)

output 이제 함수 호출에 의해 인쇄 된 행을 포함하는 목록입니다.

고급 사용법 :

분명하지 않은 것은 이것이 두 번 이상 수행 될 수 있고 결과가 연결된다는 것입니다.

with Capturing() as output:
    print 'hello world'

print 'displays on screen'

with Capturing(output) as output:  # note the constructor argument
    print 'hello world2'

print 'done'
print 'output:', output

산출:

displays on screen                     
done                                   
output: ['hello world', 'hello world2']

Update: They added redirect_stdout() to contextlib in Python 3.4 (along with redirect_stderr()). So you could use io.StringIO with that to achieve a similar result (though Capturing being a list as well as a context manager is arguably more convenient).


In python >= 3.4, contextlib contains a redirect_stdout decorator. It can be used to answer your question like so:

import io
from contextlib import redirect_stdout

f = io.StringIO()
with redirect_stdout(f):
    do_something(my_object)
out = f.getvalue()

From the docs:

Context manager for temporarily redirecting sys.stdout to another file or file-like object.

This tool adds flexibility to existing functions or classes whose output is hardwired to stdout.

For example, the output of help() normally is sent to sys.stdout. You can capture that output in a string by redirecting the output to an io.StringIO object:

  f = io.StringIO() 
  with redirect_stdout(f):
      help(pow) 
  s = f.getvalue()

To send the output of help() to a file on disk, redirect the output to a regular file:

 with open('help.txt', 'w') as f:
     with redirect_stdout(f):
         help(pow)

To send the output of help() to sys.stderr:

with redirect_stdout(sys.stderr):
    help(pow)

Note that the global side effect on sys.stdout means that this context manager is not suitable for use in library code and most threaded applications. It also has no effect on the output of subprocesses. However, it is still a useful approach for many utility scripts.

This context manager is reentrant.

참고URL : https://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call

반응형