title: Handling UTF filenames in Python
body: <p>I've read quite a bit on the topic already, including what seems to be the definitive guide on this topic here: <a href="http://docs.python.org/howto/unicode.html" rel="nofollow">http://docs.python.org/howto/unicode.html</a></p>
  
  <p>Perhaps for a more experienced developer, that guide may be enough. However, in my case, I'm more confused than when I started and still haven't resolved my issue.</p>
  
  <p>I am trying to read filenames using os.walk() and to obtain certain information about the files (such as filesize) before writing that information to a text file. This works as long as I don't run into any files with filenames encoded in utf. When it hits a file with a utf encoded name I get an error like this one:</p>
  
  <pre><code>WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'Documents\\??.txt'
  </code></pre>
  
  <p>In that case, the file was named 唽咿.txt.</p>
  
  <p>Here is how I have been trying to do it so far:</p>
  
  <pre><code>for (root, dirs, files) in os.walk(dirpath):
          for filename in files:
              filepath = os.path.join(root, filename)
              filesize = os.stat(filepath).st_size
              file = open(filepath, 'rb')
              stuff = get_stuff(filesize, file)
              file.close()
  </code></pre>
  
  <p>In case it matters, dirpath came from an earlier portion of code that amounts to 'dirpath = raw_input()'.</p>
  
  <p>I've tried various things such as changing the filepath line to:</p>
  
  <pre><code>filepath = unicode(os.path.join(unicode(root), unicode(filename)))
  </code></pre>
  
  <p>But nothing I have tried has worked.</p>
  
  <p>Here are my two questions:</p>
  
  <ol>
  <li><p>How can I get it to pass the correct filename to the os.stat() method so that I can get a correct response from it?</p></li>
  <li><p>My script needs to write some filenames into a text file that it may later want to read from. At that point it needs to be able to find the file based on what it just read from the text file. How do I write such filenames to a text file properly and then read from it properly later?</p></li>
  </ol>