2014-02-23 53 views
3

我在python腳本中搜索文件並存儲文件。 問題是,在某些情況下,在內部有特殊字符(例如UTF-8 Table hex U + 00C4 U + 00D6 U + 00DC等) 當我使用「print」打印路徑時,會顯示正確。當我使用這個 字符串將它發送到os.system()時,特殊字符被轉義出來,並且 得到一個UTF錯誤。Python:UTF-8德國特殊字符

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的幫助! 賴

copycmd = "cp -nv " + pathtoFile_src + " " + destdir 
print copycmd 
os.system(copycmd) 

回答

2

使用encode爲Unicode轉換爲字節字符串:

os.system(copycmd.encode('utf-8')) 
+0

@rainer問題 – ndpu

+0

真棒添加錯誤的追蹤! Thx的提示!它現在的工作......忘記了裏面的舊命令,所以我再次得到錯誤......現在它很好! – rainer

+0

現在是處理空白字符。 cp正在切斷那裏的命令。 – rainer