[Python-3000] about bytes

nirinA raseliarison nirina at mail.blueline.mg
Thu Jun 26 21:31:54 CEST 2008


why the following behavior:

    >>> b=b'thisisbytes'
    >>> b[0] == b't'
    False
    >>> b[0]
    116

i expected same thing as:

    >>> b.startswith(b't')
    True
    >>> b[0:1] == b't'
    True
    >>> b[0:1]
    b't'
    >>> b[:1]
    b't'

and this is a bit curious:

    >>> b[2:5]
    b'isi'
    >>> for i in b[2:5]:
        print(i)

    105
    115
    105

problems occur when filename is in bytes.

    >>> os.listdir('/usr/doc/kbd-1.08/utf')
    ['README', 'ethiopic', b'♪♬',
     'utfdemo', 'utflist']

os.stat for example works:

    >>> os.stat(b'/usr/doc/kbd-1.08/utf/♪♬')
    posix.stat_result(st_mode=33188, st_ino=1376259,
    st_dev=5633, st_nlink=1, st_uid=0, st_gid=0, st_size=295,
    st_atime=1211305274, st_mtime=1034334536, st_ctime=1190763635)

but other functions don't. especially os.path.join and os.walk complain:

    >>> w=os.walk('/usr/doc/kbd-1.08/utf/')
    >>> next(w)
    Traceback (most recent call last):
      File "<pyshell#36>", line 1, in <module>
        next(w)
      File "/root/Py3kb1/lib/python3.0/os.py", line 268, in walk
        if isdir(join(top, name)):
      File "/root/Py3kb1/lib/python3.0/posixpath.py", line 64, in join
        if b.startswith('/'):
    TypeError: expected an object with the buffer interface

and trying to open the file:

    >>> f=open('♪♬')
    Traceback (most recent call last):
      File "<pyshell#77>", line 1, in <module>
        f=open('♪♬')
      File "/root/Py3kb1/lib/python3.0/io.py", line 280, in __new__
        return open(*args, **kwargs)
      File "/root/Py3kb1/lib/python3.0/io.py", line 219, in open
        closefd)
    UnicodeEncodeError: 'ascii' codec can't encode characters in
    position 0-5: ordinal not in range(128)

    >>> f=open(b'♪♬')
    Traceback (most recent call last):
      File "<pyshell#78>", line 1, in <module>
        f=open(b'♪♬')
      File "/root/Py3kb1/lib/python3.0/io.py", line 280, in __new__
        return open(*args, **kwargs)
      File "/root/Py3kb1/lib/python3.0/io.py", line 180, in open
        raise TypeError("invalid file: %r" % file)
    TypeError: invalid file: b'♪♬'

so, how do you deal with bytes in these cases?

nirinA

--



More information about the Python-3000 mailing list