Skip to main content
added 62 characters in body
Source Link
jldupont
  • 97.6k
  • 58
  • 212
  • 328

Redirect stdout and capture its output in an object?

import sys # a simple class with a write method class WritableObject: def __init__(self): self.content = [] def write(self, string): self.content.append(string) # example with redirection of sys.stdout foo = WritableObject() # a writable object sys.stdout = foo # redirection print "one, two, three, four" # some writing 

And then just take the "output" from foo.content and do what you want with it.

Please disregard if I have misunderstood your requirement.

Redirect stdout and capture its output in an object?

import sys # a simple class with a write method class WritableObject: def __init__(self): self.content = [] def write(self, string): self.content.append(string) # example with redirection of sys.stdout foo = WritableObject() # a writable object sys.stdout = foo # redirection print "one, two, three, four" # some writing 

And then just take the "output" from foo.content and do what you want with it.

Redirect stdout and capture its output in an object?

import sys # a simple class with a write method class WritableObject: def __init__(self): self.content = [] def write(self, string): self.content.append(string) # example with redirection of sys.stdout foo = WritableObject() # a writable object sys.stdout = foo # redirection print "one, two, three, four" # some writing 

And then just take the "output" from foo.content and do what you want with it.

Please disregard if I have misunderstood your requirement.

Source Link
jldupont
  • 97.6k
  • 58
  • 212
  • 328

Redirect stdout and capture its output in an object?

import sys # a simple class with a write method class WritableObject: def __init__(self): self.content = [] def write(self, string): self.content.append(string) # example with redirection of sys.stdout foo = WritableObject() # a writable object sys.stdout = foo # redirection print "one, two, three, four" # some writing 

And then just take the "output" from foo.content and do what you want with it.