ActivePython word wrap in Shell or PythonWin during list printout

Brian Quinlan BrianQ at ActiveState.com
Thu Jul 12 01:06:06 CEST 2001


Rufus wrote:
> I am running the latest Python from ActiveState and I have a
> problem that when I print a long list (Like dir()), the entire list
> prints out on one line and I have to scroll over to read it.
>
> Is there an environment setting to force word wrap at
> a certain line length?
>
> Or is there a list function that can take a list and
> pretty print it with word wrap.

You could try something like this:

import sys

class stdoutwrapper:
    def __init__(self, width=80):
        self.stdout = sys.stdout
        self.width = width

    def write(self, buffer):
        for line in buffer.split('\n'):
            while line:
                self.stdout.write(line[0:self.width] + '\n')
                line = line[self.width:]

sys.stdout = stdoutwrapper(100)
print 'This is a very long test' * 1000

I didn't really test this so be careful. Also note how I don't do the
right thing if the buffer doesn't end in a newline.

Cheers,
Brian





More information about the Python-list mailing list