My first attempt at subclassing....Gahh!

Alex Martelli aleaxit at yahoo.com
Tue Sep 14 04:08:25 EDT 2004


Robin Siebler <robin.siebler at palmsource.com> wrote:

> I want to use filecmp.dircmp, but it only prints to the screen, there
> is no way to capture the output.  So I thought that I should simply be

Il prints to sys.stdout, which may be the screen or elsewhere, therefore
there IS of course a way to capture the output:

import sys, filecmp, cStringIO

save_stdout = sys.stdout
capture = cStringIO.StringIO()
sys.stdout = capture
filecmp.dircmp('/tmp', '/tmp').report()
sys.stdout = save_stdout

capture.seek(0)
for i, line in enumerate(capture):
    print i, repr(line)
capture.close()

But why would you want that?  The output format is purposefully lousy.
You have all the information as attributes of the object that
filecmp.dircmp returns -- why not format said info, or subsets thereof,
however best you please...?  Say:

x = filecmp.dircmp('/tmp', '/tmp')
print 'Out of %d files on one side, and %d on the other,' % (
        len(x.left_list), len(x.right_list))
print '%d are in common (%d files, %d directories, %d funny ones)' % (
        len(x.common), len(x.common_files), len(x.common_dirs),
        len(x.common_funny))

or whatever it is that you do wish.


Alex



More information about the Python-list mailing list