cStringIO.StringIO has no write method?

Steve Holden steve at holdenweb.com
Tue Jul 25 15:34:19 EDT 2006


Laszlo Nagy wrote:
> This program:
> 
> import sys
> import traceback
> import cStringIO
> 
> a = 1.0
> b = 0.0
> try:
>     c=a/b
> except:    
>     f = cStringIO.StringIO('')
>     ei = sys.exc_info()
>     traceback.print_exception(ei[0],ei[1],ei[2],file=f)
> 
> raises this exception:
> 
> Traceback (most recent call last):
>   File "C:/Documents and Settings/gandalf/test.py", line 12, in ?
>     traceback.print_exception(ei[0],ei[1],ei[2],file=f)
>   File "C:\Python24\Lib\traceback.py", line 124, in print_exception
>     _print(file, 'Traceback (most recent call last):')
>   File "C:\Python24\Lib\traceback.py", line 13, in _print
>     file.write(str+terminator)
> AttributeError: 'cStringIO.StringI' object has no attribute 'write'
> 
> What is 'cStringIO.StringI' anyway? Shouldn't it be 'cStringIO.StringIO'?
> 
Nope. StringI is an input-only object, StringO is an output object. You 
got a StringI because you gave a string argument to the creator.


 >>> f1 = cStringIO.StringIO()
 >>> f1
<cStringIO.StringO object at 0x186c9c00>
 >>> dir(f1)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', 
'__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__str__', 'close', 'closed', 'flush', 
'getvalue', 'isatty', 'next', 'read', 'readline', 'readlines', 'reset', 
'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines']
 >>> f2 = cStringIO.StringIO("This is the fixed content of the StringIO")
 >>> f2
<cStringIO.StringI object at 0x18661440>
 >>> dir(f2)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', 
'__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__str__', 'close', 'closed', 'flush', 
'getvalue', 'isatty', 'next', 'read', 'readline', 'readlines', 'reset', 
'seek', 'tell', 'truncate']
 >>>

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list