os.path.exists and unicode filenames

  • Thread starter Peter Bienstman
  • Start date
P

Peter Bienstman

Hi,

I'm trying to add support for unicode file names to my application.

I'm running into problems with e.g. the os.path.exists routine, which
complains if it gets passed a unicode string:

Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/mnemosyne/pyqt_ui/main_dlg.py", line
149, in fileNew
if os.path.exists(out):
File "/usr/lib/python2.4/posixpath.py", line 171, in exists
st = os.stat(path)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 24-29:
ordinal not in range(128)

I could try encoding the string in utg-8, but then it wouldn't work under
Windows.

Is there an elegant cross-platform solution for this?

Thanks!

Peter
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Peter said:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 24-29:
ordinal not in range(128)

I could try encoding the string in utg-8, but then it wouldn't work under
Windows.

Is there an elegant cross-platform solution for this?

I assume you are using some Unix version here. If so, you should set up
the locale correctly. For example, if you set LANG to a UTF-8 locale
supported by your operating system, this (passing Unicode strings to
os.path.exists etc) works just fine.

Python follows the Unix convention that the locale defines how to
interpret characters, in particular for file names.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top