Skip to content

Instantly share code, notes, and snippets.

@alice1017
Created February 14, 2011 01:36
Show Gist options
  • Select an option

  • Save alice1017/825368 to your computer and use it in GitHub Desktop.

Select an option

Save alice1017/825368 to your computer and use it in GitHub Desktop.
redirect a output
#!/usr/bin/python
#coding:utf-8
import sys,types
from StringIO import StringIO
class Redirect:
def __init__(self):
sys.stdout = StringIO()
self.stdout = sys.stdout
def getitem(self):
if self.check():
item = sys.stdout.getvalue()
self.stdout.close()
sys.stdout = sys.__stdout__
return item
def getitems(self):
if self.check():
item = sys.stdout.getvalue()
items = item.split("\n")
self.stdout.close()
sys.stdout = sys.__stdout__
return items
def quit(self):
if self.check(): self.stdout.close();sys.stdout = sys.__stdout__
def check(self):
if isinstance(self.stdout,types.InstanceType): return True
else:return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment