os.rename() doesn't work w/unicode??

Neil Hodgson nhodgson at bigpond.net.au
Tue Feb 15 05:20:32 EST 2005


fanbanlo:

> The filename is supposed to be in Chinese, on NTFS (WinXP).  However,
> when i print it, they are all '???', and that caused os.rename() to break.
>
> How to force os.walk to return a list of unicode filename?

   The convention in the unicode file name support is that calls with
unicode arguments return unicode results:

>>> h = "e:\\unicode"
>>> import os
>>> for p,d,f in os.walk(h):
...     print p, "!", d, "!", f
...
e:\unicode ! [] ! ['ko.py', 'abc', 'ascii', 'Gr\xfc\xdf-Gott', 'uni.py',
'Ge??-sa?', '????????????', '??????', '???', '????G\xdf', '???',
'unilin.py']
>>> for p,d,f in os.walk(unicode(h, "latin1")):
...     print p, "!", d, "!", f
...
e:\unicode ! [] ! [u'ko.py', u'abc', u'ascii', u'Gr\xfc\xdf-Gott',
u'uni.py', u'\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
u'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
u'\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1', u'\u306b\u307d\u3093',
u'\u66e8\u05e9\u3093\u0434\u0393\xdf', u'\u66e8\u66e9\u66eb', u'unilin.py']

   Neil





More information about the Python-list mailing list