Reputation: 173
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)
Upvotes: 3
Views: 2912
Reputation: 22571
Use encode
to convert unicode to byte string:
os.system(copycmd.encode('utf-8'))
Upvotes: 2