[Tutor] Unicode filenames (Windows)

Kent Johnson kent37 at tds.net
Tue Jun 20 17:52:54 CEST 2006

Terry Carroll wrote:
> 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
> ????.test
>>>> 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.

I'm guessing that when PEP 277 refers to the posix module maybe it means 
the os module. What happens if you pass a Unicode string to listdir(), 
e.g. os.listdir(u'.')?

Hmm, looks promising:
In [3]: os.listdir(u'.')
Out[3]: [u'enterData.py', u'test.py', u'SPY2.csv', u'\xe1\xe9\xed\xf3\xfa']

In [4]: p=_[3]

In [5]: p
Out[5]: u'\xe1\xe9\xed\xf3\xfa'

In [6]: os.rename(p, u'foo')

In [7]: os.listdir(u'.')
Out[7]: [u'enterData.py', u'test.py', u'SPY2.csv', u'foo']


More information about the Tutor mailing list