[Tutor] Unicode filenames (Windows)

Terry Carroll carroll at tjc.com
Tue Jun 20 16:27:49 CEST 2006

Is it possible to process files with Unicode filenames in Windows?

Ultimately, I want to read in a series of filenames, and rename them (to 
remove the unicode characters).

In this test, I have one file whose name is XXXX.test, where "XXXX" are 
unicode characters.

First attempt:

>>> import os
>>> file_list = [x for x in os.listdir('.') if x.endswith('test')]
>>> oldname = file_list[0]
>>> newname = "spam.test"
>>> print oldname
>>> os.rename(oldname,newname)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OSError: [Errno 22] Invalid argument

PEP 277, http://www.python.org/dev/peps/pep-0277/ , suggests that Windows
Unicode filename support was added in Python 2.3 (I'm on 2.4), using the 
posix module, but no joy: no posix module on my Windows XP install.

>>> import posix
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named posix

More information about the Tutor mailing list