“Python Capture Stdout” Code-Antworten

Python schreiben Subprozess StDout Stderr in Datei

with open("stdout.txt","wb") as out, open("stderr.txt","wb") as err:
    subprocess.Popen("ls",stdout=out,stderr=err)
Outrageous Octopus

Python Capture Stdout

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:
    print('hello world')

print('displays on screen')

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

print('done')
print('output:', output)
Super Shrew

Ähnliche Antworten wie “Python Capture Stdout”

Fragen ähnlich wie “Python Capture Stdout”

Weitere verwandte Antworten zu “Python Capture Stdout” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen