"print" strange with Sybase module
Fredrik Lundh
fredrik at effbot.org
Sun Jan 28 09:11:06 EST 2001
Erwin S. Andreasen wrote:
> AFAICS, there is no way to turn the buffering to be line-based on an existing
> file object (it might be an idea for a future release?). You could do
> something like:
>
> sys.stdout = os.fdopen(os.dup(sys.stdout.fileno()), 'w', 1)
>
> which would reopen standard output as a line-buffered (the 1) stream,
> i.e. every time you have written a full line to the file object, it's flushed
> -- just like when you run the program on the console.
here's a variant, which works for any kind of sys.stdout:
class UnbufferedStream:
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def __getattr__(self, attr):
# delegate everything else
return getattr(self.stream, attr)
sys.stdout = UnbufferedStream(sys.stdout)
Cheers /F
<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list