[Python-Dev] Filename as byte string in python 2.6 or 3.0?

Victor Stinner victor.stinner at haypocalc.com
Sat Sep 27 16:44:50 CEST 2008


Le Saturday 27 September 2008 14:04:25 Victor Stinner, vous avez écrit :
> I read that Python 2.6 is planned to Wednesday. One bug is is still open
> and important for me: Python 2.6/3.0 are unable to use filename as byte
> strings. http://bugs.python.org/issue3187

Ooops, as amaury noticed, the problem is specific to Python 3.0. My example 
works correctly with Python 2.6:
----------
$ find
.
./a?b
./dir?name
./dir?name/file

$ ~/prog/python-trunk/python
Python 2.6rc2+ (trunk:66627M, Sep 26 2008, 19:03:31)
>>> import os, shutil
>>> os.listdir('.')
['a\xffb', 'dir\xffname']
>>> open(os.listdir('.')[0]).close()
>>> os.unlink(os.listdir('.')[0])
>>> os.listdir('.')
['dir\xffname']
>>> shutil.rmtree(os.listdir('.')[0])
----------


Same test with Python 3.0:
----------
$ pwd
/tmp/test

$ find
.
./a?b
./dir?name
./dir?name/file

$ ~/prog/py3k/python
Python 3.0rc1+ (py3k:66627M, Sep 26 2008, 18:10:03)
>>> import os, shutil
>>> os.listdir('.')
[b'a\xffb', b'dir\xffname']
>>> open(os.listdir('.')[0]).close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NOT FOUNT
>>> os.unlink(os.listdir('.')[0])
>>> os.listdir('.')
[b'dir\xffname']
>>> shutil.rmtree(os.listdir('.')[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NOT FOUNT
----------

Results:
 * open() doesn't support bytes
 * unlink() supports bytes
 * shutil.rmtree() doesn't support bytes


Another example to test chdir()/getcwd():
----------
$ pwd
/tmp/test

$ ~/prog/py3k/python
Python 3.0rc1+ (py3k:66627M, Sep 26 2008, 18:10:03)
>>> import os, shutil
>>> os.getcwd()
'/tmp/test'
>>> os.chdir(b'/tmp/test/dir\xffname')
>>> os.getcwd()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NOT FOUNT
----------

Results:
 * chdir() supports byte filename
 * getcwd() fails

-- 
Victor Stinner aka haypo
http://www.haypocalc.com/blog/


More information about the Python-Dev mailing list