I'm walking through a directory and want to write all files names into a file. Here's the piece of code
with open("c:/Users/me/filename.txt", "a") as d:
for dir, subdirs, files in os.walk("c:/temp"):
for f in files:
fname = os.path.join(dir, f)
print fname
d.write(fname + "\n")
d.close()
The problem I have is, there are some files that are named in Chinese characters. By using print
, I can see the file name correctly in console, but in the target file, it's just a mess... I've tried to open the file like open(u"c:/Users/me/filename.txt", "a")
, but it did not work. I also tried to write fname.decode("utf-16")
, still does not work...