I am recursing through folders and gathering the document names and some other data to be loaded into a database.
import os
text_file = open("Output.txt", "w")
dirName = 'D:\\'
for nextDir, subDir, fileList in os.walk(dirName):
for fname in fileList:
text_file.write(fname + '\n')
The problem is that some document names have foreign characters like:
RC-0964_1000 Tưởng thưởng Diamond trẻ nhất Việt Nam - Đặng Việt Thắng và Trần Thu Phương
And
RC-1046 安麗2013ARTISTRY冰上雅姿盛典-愛里歐娜.薩維琴科_羅賓.索爾科維【Suit & Tie】.mp4
And the code above gives me this error on the last line:
UnicodeEncodeError: 'charmap' codec can't encode characters at positions ##-##:character maps to (undefined)
I've tried to
temp = fname.endcode(utf-8)
temp = fname.decode(utf-8)
temp = fname.encode('ascii','ignore') temp2 = temp.decode('ascii')
temp =unicode(fname).encode('utf8')
How can I write this script to write all characters to the file? Do I need to change the file I'm writing to or the string I'm writing, and how?
These names can be pasted into the file successfully, so why won't Python write them in?