Why this Error may happend? I have to readjust huge application and I need the most supposed possibilities which make this kind of error.

Recommended Answers

You are trying to use string as file. If that is your purpose use the cStringIO module.

Jump to Post

[CODE]>>> x = 'car'

x.close()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'str' object has no attribute 'close'[/CODE]
As tony mention str has no close() method as a file object.

cStringIO module has close(),or maybe you are trying to …

Jump to Post

All 3 Replies

You are trying to use string as file. If that is your purpose use the cStringIO module.

>>> x = 'car'
>>> x.close()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: 'str' object has no attribute 'close'

As tony mention str has no close() method as a file object.

cStringIO module has close(),or maybe you are trying to use close on what you think is a file object.
You can check type like this.

>>> x = 'car'
>>> type(x)
<type 'str'>
>>>
#cStringIO demo
>>> from cStringIO import StringIO
>>> output = StringIO()
>>> output.write('This goes into the buffer.\n')
>>> output.write('car')
>>> print output.getvalue()
This goes into the buffer.
car
>>> output.close() # discard buffer memory

thanks for your respondings. I solved my problem with your big help ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.