[Tutor] Opening filenames with unicode characters

Tim Golden mail at timgolden.me.uk
Thu Jun 28 19:39:31 CEST 2012

On 28/06/2012 18:19, James Chapman wrote:
> Hi there python list.
> I'm trying to open a text file named "This is_a-test'FILE to Ensure$
> that£ stuff^ works.txt" (without the quotes) but I'm struggling to
> find a way to open it.

Happily, you're using Windows, which makes this very much easier.

Short Explanation

Do this: open (u"blah £3.50.txt").read () # note the u- prefix

Long Explanation:

There's way too many levels of indirection between you and the
disk to go into it all, but basically you're telling Python to
create a (byte) string from those characters using its default
encoding for code, which is UTF-8. UTF-8 maps "£" to the
one-byte 0x9c. Somewhere in the layers between that string and
the representation on disk, a mismatch occurs.


More information about the Tutor mailing list