unicode, filenames and umlaut...

C. Meyer kgmeyer at comundo.de
Thu Jun 7 12:51:20 EDT 2001


Hello,

i am new to Python. I have a problem with umlaut in filenames.

First a small sample which works fine:

Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> from tkFileDialog import *
>>> import fileinput
>>> fn = askopenfilename()
>>> fn
'C:/Berlin.txt'
>>> print fn
C:/Berlin.txt
>>> open(fn)
<open file 'C:/Berlin.txt', mode 'r' at 007DC9B8>
>>> for line in fileinput.input(fn): print line
...
BERLIN, Berlin


Same sample, but now "München.txt" selected in askopenfilename():

>>> fn = askopenfilename()
>>> fn
u'C:/M\374nchen.txt'
>>> print fn
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)

>>> open(fn)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)

>>> for line in fileinput.input(fn): print line
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "c:\programme\python20\lib\fileinput.py", line 158, in __getitem__
    line = self.readline()
  File "c:\programme\python20\lib\fileinput.py", line 227, in readline
    self._file = open(self._filename, "r")
IOError: [Errno 2] No such file or directory: 'C'


Hm, if the filename contains umlauts, askopenfilename() returns an unicode.
But print, open and fileinput.input don't like it?

What ist the "correct" solution for this problem?
A hint may help... (but fn.encode("LATIN-1") don't help)


Another surprise (for me):
If i use the new Option -U (Force Python to interpret all string literals as
Unicode literals), i could not import any module!!??

C:\>python.exe -U
Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import string
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named string
>>> from tkFileDialog import *
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named tkFileDialog

Thanks





More information about the Python-list mailing list