title: How to write Chinese characters to file by python
body: <p>I'm walking through a directory and want to write all files names into a file.  Here's the piece of code</p>
  
  <pre><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()
  </code></pre>
  
  <p>The problem I have is, there are some files that are named in Chinese characters.  By using <code>print</code>, 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 <code>open(u"c:/Users/me/filename.txt", "a")</code>, but it did not work.  I also tried to write <code>fname.decode("utf-16")</code>, still does not work...</p>