title: How can Python check if a file name is in UTF8?
body: <p>I have a PHP script that creates a list of files in a directory, however, PHP can see only file names in English and totally ignores file names in other languages, such as Russian or Asian languages.</p>
  
  <p>After lots of efforts I found the only solution that could work for me - using a python script that renames the files to UTF8, so the PHP script can process them after that.</p>
  
  <p>(After PHP has finished processing the files, I rename the files to English, I don't keep them in UTF8). </p>
  
  <p>I used the following python script, that works fine:</p>
  
  <pre><code>import sys
  import os
  import glob
  import ntpath
  from random import randint
  
  for infile in glob.glob( os.path.join('C:\\MyFiles', u'*') ):
      if os.path.isfile(infile):
        infile_utf8 = infile.encode('utf8')
        os.rename(infile, infile_utf8)
  </code></pre>
  
  <p>The problem is that it converts also file names that are already in UTF8. I need a way to skip the conversion in case the file name is already in UTF8.</p>
  
  <p>I was trying this python script:</p>
  
  <pre><code>for infile in glob.glob( os.path.join('C:\\MyFiles', u'*') ):
      if os.path.isfile(infile):
        try:
          infile.decode('UTF-8', 'strict')
        except UnicodeDecodeError:
          infile_utf8 = infile.encode('utf8')
          os.rename(infile, infile_utf8)    
  </code></pre>
  
  <p>But, if file name is already in utf8, I get fatal error:</p>
  
  <pre><code>UnicodeDecodeError: 'ascii' codec can't decode characters in position 18-20
  ordinal not in range(128)
  </code></pre>
  
  <p>I also tried another way, which also didn't work:</p>
  
  <pre><code>for infile in glob.glob( os.path.join('C:\\MyFiles', u'*') ):
      if os.path.isfile(infile):
        try:
          tmpstr = str(infile)
        except UnicodeDecodeError:
          infile_utf8 = infile.encode('utf8')
          os.rename(infile, infile_utf8)      
  </code></pre>
  
  <p>I got exactly the same error as before.</p>
  
  <p>Any ideas?</p>
  
  <p>Python is very new to me, and it is a huge effort for me to debug even a simple script, so please write an explicit answer (i.e. code). I don't have the ability of testing general ideas that maybe work or maybe not. Thanks.</p>
  
  <p>Examples of file names:</p>
  
  <pre><code> hello.txt
   你好.txt
   안녕하세요.html
   chào.doc
  </code></pre>