title: Can't open a file with a Japanese filename in Python
body: <p>Why doesn't this work in the Python interpreter? I am running the Python 2.7 version of python.exe on Windows 7. My locale is en_GB.</p>
  
  <pre><code>open(u'黒色.txt')
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  IOError: [Errno 22] invalid mode ('r') or filename: u'??.txt'
  </code></pre>
  
  <p>The file does exist, and is readable.</p>
  
  <p>And if I try</p>
  
  <pre><code>name = u'黒色.txt'
  name
  </code></pre>
  
  <p>the interpreter shows</p>
  
  <pre><code>u'??.txt'
  </code></pre>
  
  <p>Additional:</p>
  
  <p>Okay, I was trying to simplify my problem for the purposes of this forum. Originally the filename was arriving in a cgi script from a web page with a file picker. The idea was to let the web page user upload files to a server:</p>
  
  <pre><code>import cgi
  form = cgi.FieldStorage()
  fileItems = form['attachment[]']
  
  for fileItem in fileItems:
      if fileItem.file:
          fileName = os.path.split(fileItem.filename)[1]
  
          f = open(fileName, 'wb')
          while True:
              chunk = fileItem.file.read(100000)
              if not chunk: 
                  break
              f.write(chunk)
          f.close()
  </code></pre>
  
  <p>but the files created at the server side had corrupted names. I started investigating this in the Python interpreter, reproduced the problem (so I thought), and that is what I put into my original question. However, I think now that I managed to create a separate problem.</p>
  
  <p>Thanks to the answers below, I fixed the cgi script by making sure the file name is treated as unicode:</p>
  
  <pre><code>fileName = unicode(os.path.split(fileItem.filename)[1])
  </code></pre>
  
  <p>I never got my example in the interpreter to work. I suspect that is because my PC has the wrong locale for this.</p>