Hello there, i have this problem when i try to combine a variable file name that is in languages other than English with a path, like this :

path = 'C:\\Files\\'

path+=UnicodeName+'.txt'

it gives the following error :

UnicodeDecodeError: 'ascii' codec can't decode byte 0xda in position 7: ordinal not in range(128).

Thanks for your help in advance.

Dani AI

Generated

Concise explanation: in Python 2, concatenating a unicode object with a byte str forces Python to convert the str to unicode using the default ASCII codec. If that str contains bytes outside 0x00–0x7F, the conversion fails with a UnicodeDecodeError. That explains why saw no exception when the byte part was pure ASCII but hit an error in a different case.

Common, reliable fixes:

  • Make both operands Unicode. Decode incoming byte filenames with the correct encoding (the filesystem encoding is a good default) and use Unicode literals for your path pieces.
  • Use sys.getfilesystemencoding() to choose the right decoder rather than assuming UTF-8 or changing the interpreter default.
  • Prefer os.path.join for building paths so you don't accidentally mix separators and encodings.

Example pattern (adjust the encoding to your environment):

import sys, os

fsenc = sys.getfilesystemencoding() or 'utf-8'
filename_unicode = bytes_filename.decode(fsenc)
folder_unicode = u"C:\\Files"   # ensure this is a unicode object
fullpath = os.path.join(folder_unicode, filename_unicode + u".txt")

Notes and cautions: do not use the sys.setdefaultencoding hack; it only hides problems. If moving to maintenance or new development, use Python 3 where str is Unicode by default. For more background and authoritative guidance, see the Python 2 Unicode HOWTO and the sys.getfilesystemencoding() docs (Unicode HOWTO, sys.getfilesystemencoding).

Recommended Answers

All 2 Replies

I'm running python 2.6, and it does not raise an exception, instead, it converts path to unicode:

>>> path = 'C:\\Files\\'
>>> UnicodeName = u"\u2344"
>>> path+=UnicodeName+'.txt'
>>> path
u'C:\\Files\\\u2344.txt'
>>> print path
C:\Files\⍄.txt

There must be something missing in your example. Can you post console output ?

Thank you, problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.