[Tutor] Opening filenames with unicode characters
James Chapman
james at uplinkzero.com
Thu Jun 28 19:55:09 CEST 2012
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)
At Thursday, 28/06/2012 on 18:39 Tim Golden wrote:
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.
TJG
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120628/ed38eb3b/attachment.html>
More information about the Tutor
mailing list