python – AttributeError: StringIO instance has no attribute ‘fileno’

Are you using the standard plain python interpreter? This error may appear when you use an interpreter that overrides stdout/stderr, such as IDLE (though IDLE itself would give you a different error). It may also be caused by a library which overrides stdout/stderr.

Sometimes you can reset your stdout the the default stdout by writing sys.stdout = sys.__stdout__, but don’t count on it working always. It doesn’t work in Pythonwin for instance.

Anyway, it seems that what you’re trying to do with your code is to redirect stdout/stderr yourself. If that’s the case, you should just go ahead and do it. I think this should work, if you have file descriptors outfd and errfd:

sys.stdout = os.fdopen(outfd, 'w')
sys.stderr = os.fdopen(errfd, 'w')


Now that I can see your entire code, I wouldn’t use temporary files at all.

def captureOutput(self, func, *args, **kwargs):
    import cStringIO # You can also use StringIO instead

    olderr, oldout = sys.stderr, sys.stdout
        sys.stderr = cStringIO.StringIO()
        sys.stdout = cStringIO.StringIO()
            ret = func(*args, **kwargs)
            err =
            out =
        sys.stderr = olderr
        sys.stdout = oldout

    return ret, out, err

Facebook Comments


(Visited 3 times, 1 visits today)