I'm developing my QGIS-plugin. Unfortunately I faced a problem I can't solve. Plugin works pretty fine, until user hendles non-ascii characters inside input or output paths.

The problem occures when GDAL functions (gdalwarp, gdal_translate) are being executed via os.system(). I read a lot about handling non-ascii characters, but I still can't fix that bug. .encode('utf8') and .decode('utf8') don't help in this case.

I still get the following error messages:

When I'm using Python:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position47: ordinal not in range(128)

When I try to run the problematic functions via OSGeo4W Shell:

ERROR 4: `E:/projekty/python/dane/ŕ╣│┐衊˝_raster.jpg' does not exist in the file system

So, according to what I've already written. How can one improve the following code, so it will not lead to error whenever non-ascii characters are or are not used within files paths?

import os

def do_something(input_path, output_path):

    os.system('gdalwarp -co COMPRESS=LZW "' + input_path + '" "' + output_path +'"')
  • First check this is anything to do with GDAL. What happens when you do os.listdir(input_directory)? Can you see the file? Can you work with it using an index from the resulting list? Can you do trivial Python operations on the file like open() using this reference? Then test these characters using filenames that use one character at a time to narrow down what is causing the problem. – Alexander Mar 21 '17 at 17:49
  • Yep, I've tried it. it doesn't work, in Python... ;/ But I need to solve the problem for all characters, not for just a specific one. The tool should be universal. But the problem is not only in handling paths to OS via Python, but then to GDAL, which, as I suppose, uses some other kind of coding than OS. 'OSError: [Errno 22] Invalid argument: 'E:\\projekty\\python\\janek_georeferencer\\dane\x0cig\xb3.jpg.points' – Janek Mar 23 '17 at 6:59
  • Your best option might be renaming the files first. I don't know what os your using but a quick search for 'remove invalid character from filenames' brings up plenty of results. – Alexander Mar 23 '17 at 9:36
  • QGIS Python uses OSGeo4W Shell and I'm running it on windows 7. But the point is that is should work on xp,vista, 7, 10, Mac, linux and so on. Renaming the files is the option but it's not a good solution for a plugin. So far I i made only an information dialog that tells the user that nonASCII characters are not supported – Janek Mar 29 '17 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.