lagoru
lagoru

Reputation: 707

How to force python functions to work with UTF-8

I have problem with python function os.path.isdir

While I'm trying to use it I get: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 36: ordinal not in range(128)

I've already placed coding "stamp" in the header of my file #!/usr/bin/env python # coding: utf-8

I also use rather proper string decoding that enables getting utf-8 signs (I load them by QT QLineEdit - but this does not matter).

tmp_filepath = u''
tmp_filepath = tmp_filepath.decode('utf-8')
tmp_filepath += QtGui.QFileDialog.getExistingDirectory(self,"Choose directory",self.directorypath,QtGui.QFileDialog.ShowDirsOnly)

Problem occurs while I'm trying to use: os.path.isdir(tmp_filepath)

I've read that this may be caused by bad python version (non utf-8) but I couldn't find other info about this. I use python 2.6.5 on Linux Ubuntu 10.04.

Upvotes: 1

Views: 3722

Answers (2)

jsbueno
jsbueno

Reputation: 110311

Qt returns you a QString object - you have to trasform it to a Python unicode and encode it into utf-8:

 tmp_filepath = unicode(tmp_filepath)
 os.path.isdir(tmp_filepath.encode("utf-8"))

Also, be shure to read http://www.joelonsoftware.com/articles/Unicode.html before proceeding with your programing today.

Alternatively if you don't have to interoperate with other text variables in Python, QString objects provide a .toUtf8 method themselves:

os.path.isdir(tmp_filepath.toUtf8()) 

Upvotes: 0

AJM
AJM

Reputation: 46

isdir wants to convert its argument to a byte sequence (str) because the underlying file system uses byte sequences as file names. If you supply a character string (unicode) it must encode it somehow.

It uses the same encoding rule that the print commmand would use. Try print tmp_filepath and you will probably get the same exception.

To solve this problem, either (a) set your locale (e.g LANG=en_US.utf-8 in the environment) or (b) pass tmp_filename.encode('utf-8') to isdir and mkdir.

I recommend (a).

Upvotes: 3

Related Questions