[Python-Dev] Unicode strings as filenames

Neil Hodgson nhodgson@bigpond.net.au
Mon, 7 Jan 2002 10:19:11 +1100


This is a multi-part message in MIME format.

------=_NextPart_000_023E_01C19764.BFBE5450
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Martin v. Loewis:
> I'll attach a script below. It contains UTF-8 encoded data, so to
> prevent transmission errors, it comes base-64 attached. Running it
> creates a three additional files in the current directory; I recommend
> to run it in an empty directory.

   I have added some more cases to your example Martin, in Hebrew, Chinese
and Japanese and a combination. The combination is an interesting case as it
will not work with mbcs with a particular code page, as no code page (to my
knowledge) contains all the characters.

   This works using my modifications except for the calls to os.rename.

   Neil

------=_NextPart_000_023E_01C19764.BFBE5450
Content-Type: text/plain;
	name="uni.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="uni.py"

# -*- coding: utf-8 -*-=0A=
import locale, os=0A=
=0A=
locale.setlocale(locale.LC_ALL, "")=0A=
=0A=
filenames =3D [=0A=
    unicode("Gr=C3=BC=C3=9F-Gott","utf-8"),=0A=
    unicode("=CE=93=CE=B5=CE=B9=CE=AC-=CF=83=CE=B1=CF=82","utf-8"),=0A=
    =
unicode("=D0=97=D0=B4=D1=80=D0=B0=D0=B2=D1=81=D1=82=D0=B2=D1=83=D0=B9=D1=82=
=D0=B5","utf-8"),
    unicode("=E3=81=AB=E3=81=BD=E3=82=93","utf-8"),
    unicode("=D7=94=D7=A9=D7=A7=D7=A6=D7=A5=D7=A1","utf-8"),=0A=
    unicode("=E6=9B=A8=E6=9B=A9=E6=9B=AB","utf-8"),=0A=
    unicode("=E6=9B=A8=D7=A9=E3=82=93=D0=B4=CE=93=C3=9F","utf-8"),=0A=
    ]=0A=
=0A=
for name in filenames:=0A=
    print repr(name)=0A=
    f =3D open(name, 'w')
    f.write((name+u'\n').encode("utf-8"))=0A=
    f.close()=0A=
    os.stat(name)=0A=
=0A=
print os.listdir(".")=0A=
=0A=
for name in filenames:=0A=
    os.rename(name,"tmp")=0A=
    os.rename("tmp",name)=0A=
    =0A=

------=_NextPart_000_023E_01C19764.BFBE5450--