Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: UTF-8 german special chars

Tags:

python

utf

I'm searching for files in a python script and storing the filepathes. The problem is, that in some cases there are special chars like ö ä ü inside (UTF-8 Table hex U+00C4 U+00D6 U+00DC etc.) When I print the path with "print" it is shown correctly. When I use this string for sending it to os.system() the special chars are escaped out and getting an UTF error.

ErrorMsg:

cp -nv /home/rainer/Arbeitsfläche/Videofiles/A047C001_130226_R1WV.mov /media/rainer/LinuxData
Traceback (most recent call last):
  File "Clipfinder.py", line 254, in <module>
    copyProcess(sourcedir,destdir,cliplist)
  File "Clipfinder.py", line 205, in copyProcess
    os.system(copycmd)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 29: ordinal not in range(128)

Thx for help ! rainer

copycmd = "cp -nv " + pathtoFile_src + " " + destdir
print copycmd
os.system(copycmd)
like image 773
rainer Avatar asked Feb 25 '26 02:02

rainer


1 Answers

Use encode to convert unicode to byte string:

os.system(copycmd.encode('utf-8'))
like image 62
ndpu Avatar answered Feb 27 '26 01:02

ndpu