[Tutor] Opening filenames with unicode characters

James Chapman james at uplinkzero.com
Thu Jun 28 19:19:54 CEST 2012


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.

>>> filename = "This is_a-test'FILE to Ensure$ that£ stuff^
works.txt.js"
>>> open(filename)
Traceback (most recent call last):
  File "", line 1, in 
IOError: [Errno 2] No such file or directory: "This is_a-test'FILE to
Ensure$ that\x9c stuff^ works.txt.js"

>>> import codecs
>>> codecs.open(filename, 'r', 'utf-8')
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python27\lib\codecs.py", line 881, in open
    file = __builtin__.open(filename, mode, buffering)
IOError: [Errno 2] No such file or directory: "This is_a-test'FILE to
Ensure$ that\x9c stuff^ works.txt.js"

>>> os.listdir(".")
["This is_a-test'FILE to Ensure$ that\xa3 stuff^ works.txt.js"]

>>> filename.decode()
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position
35: ordinal not in range(128)

I've tried all sorts of encode and decode methods on the string
containing the file name but nothing seems to be working. 

Any help would be appreciated.

James

PS: This is on 64bit Windows with ActivePython 2.7.3


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


More information about the Tutor mailing list