title: Python: How to use shutil.copy() with unicode filenames
body: <p>So I've been pounding my head against this problem for days and I just can't figure it out.  I've read <a href="https://docs.python.org/2/howto/unicode.html" rel="nofollow noreferrer">this</a>, <a href="https://stackoverflow.com/questions/5523373/python-how-to-move-a-file-with-unicode-filename-to-a-unicode-folder">this</a> and <a href="https://stackoverflow.com/questions/4173477/copying-files-with-unicode-names">this</a> and feel like I must be missing something.</p>
  
  <p>I'm trying to copy a simple text file with a complex unicode title into a temp folder with this code:</p>
  
  <pre><code>self._temp_path = tempfile.mkdtemp()
  self.src = os.path.join(self._temp_path, 'src')
  os.makedirs(self.src)
  self.dst = os.path.join(self._temp_path, 'dst')
  os.makedirs(self.dst)
  self.dirname = dirname = os.path.join(os.path.dirname(__file__), 'testfiles')
  f = u'file-\xe3\x82\xa8\xe3\x83\xb3\xe3\x83\x89\xe3\x83\xac\xe3\x82\xb9.txt'
  src = os.path.join(dirname, f)
  dst = os.path.join(self.src, f)
  shutil.copy2(src, dst)
  </code></pre>
  
  <p>And I receive the following message when I execute the test:</p>
  
  <pre><code>s = '/tmp/tmpc1gzwf/src/file-ã¨ã³ãã¬ã¹.txt'
  &gt;           st = os.stat(s)
  E           UnicodeEncodeError: 'ascii' codec can't encode characters in position 24-38: ordinal not in range(128)
  </code></pre>
  
  <p>I've tried using both shutil.copy and shutil.copy2, they produced identical results.  I've also tried changing:</p>
  
  <pre><code>shutil.copy2(src, dst)
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>shutil.copy2(src.encode('utf-8'), dst.encode('utf-8'))
  </code></pre>
  
  <p>But that resulted in this error message, due to the encoding mangling the filename:</p>
  
  <pre><code>src = '/home/phil/projects/unicode_copy/tests/testfiles/file-\xc3\xa3\xc2\x82\xc2\xa8\xc3\xa3\xc2\x83\xc2\xb3\xc3\xa3\xc2\x83\xc2\x89\xc3\xa3\xc2\x83\xc2\xac\xc3\xa3\xc2\x82\xc2\xb9.txt'
  dst = '/tmp/tmpCsb3qW/src/file-\xc3\xa3\xc2\x82\xc2\xa8\xc3\xa3\xc2\x83\xc2\xb3\xc3\xa3\xc2\x83\xc2\x89\xc3\xa3\xc2\x83\xc2\xac\xc3\xa3\xc2\x82\xc2\xb9.txt'
  def copyfile(src, dst):
  ...
  &gt;       with open(src, 'rb') as fsrc:
  E       IOError: [Errno 2] No such file or directory: '/home/phil/projects/unicode_copy/tests/testfiles/file-\xc3\xa3\xc2\x82\xc2\xa8\xc3\xa3\xc2\x83\xc2\xb3\xc3\xa3\xc2\x83\xc2\x89\xc3\xa3\xc2\x83\xc2\xac\xc3\xa3\xc2\x82\xc2\xb9.txt'
  </code></pre>
  
  <p>After trying many other combinations of encode() and decode() at various points in the code, I've given up.  What is the proper way to declare a unicode filename and pass it to shutil.copy?</p>