Error or function: open and io.open are not interchangeable

I always thought that openand io.openare interchangeable.
Apparently not if I believe in this snippet:

import ctypes, io

class POINT(ctypes.Structure):
    _fields_ = [("x", ctypes.c_int),("y", ctypes.c_int)]
# THIS WORKS
with open("mypoints.bin", "wb") as f: 
    for i in range(10):
        p = POINT(i,10-i)
        print p.x, p.y
        f.write(p)
# THIS FAILS 
with io.open("mypoints.bin", "wb") as f:
    for i in range(10):
        p = POINT(i,10-i)
        print p.x, p.y
        f.write(p)

0 10
Traceback (most recent call last):
  File "D:\test.py", line 10, in <module>
    f.write(p)
  File "c:\Python26\lib\io.py", line 1070, in write
    self._write_buf.extend(b)
TypeError: 'POINT' object is not iterable

Note. I tested in Python 2.6.6

+5
source share
1 answer

, "", io.open Python 2.6 . , 3.x open, , . , , . Python 2.7, open open 3.x, io.open - .

, open, 2.x 3.x, , 2.x , (, unicode ). , codecs.open io.open encoding. 3.x.

, open io.open , io Python 3 io, Python 3 open open Python 2.6 .

http://docs.python.org/library/io.html

+7

All Articles