3

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)
rainer
  • 173
  • 1
  • 3
  • 9

1 Answers1

2

Use encode to convert unicode to byte string:

os.system(copycmd.encode('utf-8'))
ndpu
  • 22,225
  • 6
  • 54
  • 69
  • awesome! Thx for the hint!!! its working now... forgot the old command inside, so i got the error again... now its fine!!!! – rainer Feb 23 '14 at 17:30
  • Now it's to deal with whitespace chars. cp is cutting up the command there. – rainer Feb 23 '14 at 17:58
  • I'm escaping the white-chars with the replace function which works fine. But I have another problem now. When I'm adding a string with special chars into a list getting an error: Scanning Source Directory /home/rainer/Arbeitsfläche/Videofiles test for Files. This can take some time! Traceback (most recent call last): File "Clipfinder_RAW.py", line 346, in copyProcess(sourcedir,destdir,cliplist) File "Clipfinder_RAW.py", line 238, in copyProcess found_srcdirs.encode('utf-8') UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 22: ordinal not in range(128) – rainer Mar 02 '14 at 13:19
  • Before writing it into the list: /home/rainer/Arbeitsfläche/Videofiles test/A051R1WV/A051C001_130227_R1WV.mov and then when its in list: '/home/rainer/Arbeitsfl\xc3\xa4che/Videofiles test/A051R1WV' – rainer Mar 02 '14 at 13:33
  • @rainer your should decode string before encoding (you already have encoded byte string): `print '/home/rainer/Arbeitsfl\xc3\xa4che/Videofiles test/A051R1WV'.decode('utf-8') /home/rainer/Arbeitsfläche/Videofiles test/A051R1WV` – ndpu Mar 02 '14 at 13:42
  • I'm getting this error then: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 22: ordinal not in range(128) – rainer Mar 02 '14 at 13:53
  • when I read the list with: for entries in found_dirs: print entries I'm getting the correct output. with: print stringlist[0:] its not decoded. – rainer Mar 02 '14 at 14:15
  • @rainer you can post another question, put exception traceback and code in which you got exception in it – ndpu Mar 02 '14 at 14:23