[Tutor] Opening filenames with unicode characters

James Chapman james at uplinkzero.com
Thu Jun 28 20:55:06 CEST 2012


Why can I not convert my existing byte string into a unicode string?

In the mean time I'll create my original string as unicode and see if
that solves my problem.

>>> fileName = unicode(filename)
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position
35: invalid start byte

--
James

At Thursday, 28/06/2012 on 18:58 Jerry Hill wrote:

On Thu, Jun 28, 2012 at 1:55 PM, James Chapman  wrote:
> Thanks Tim, while this works, I need the name to be stored in a
variable as
> it's dynamic.
>
> In other words, how do I rewrite
> open(u"blah£.txt")
>
> to be
> filename = "blah£.txt"
> open(filename)

You have to create a unicode-string, not a byte-string.  So, like
this:

filename = u"blah£.txt"
open(filename)

-- 
Jerry


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120628/45ea3ce9/attachment.html>


More information about the Tutor mailing list