您的错误路径为:
'xe3x82xafxe3x83xacxe3x82xa4xe3x82xb8xe3x83xbcxe3x83xbbxe3x83x92xe3x83x83xe3x83x84xefxbcx81'
我认为这是文件名的UTF8编码版本。
我在Windows7上创建了一个同名文件夹,并在其中放置了一个名为“ abc.txt”的文件:
>>> a = 'xe3x82xafxe3x83xacxe3x82xa4xe3x82xb8xe3x83xbcxe3x83xbbxe3x83x92xe3x83x83xe3x83x84xefxbcx81'>>> os.listdir('.')['?????xb7???!']>>> os.listdir(u'.') # Pass unipre to have unipre returned to you[u'u30afu30ecu30a4u30b8u30fcu30fbu30d2u30c3u30c4uff01']>>> >>> a.depre('utf8') # UTF8 decoding your string matches the listdir outputu'u30afu30ecu30a4u30b8u30fcu30fbu30d2u30c3u30c4uff01'>>> os.listdir(a.depre('utf8'))[u'abc.txt']因此,邓肯的建议似乎可以解决问题
path.depre('utf8')。更新资料
我无法为您进行测试,但是建议您在执行之前尝试检查路径是否包含非ascii
.depre('utf8')。这有点hacky …ASCII_TRANS = '_'*32 + ''.join([chr(x) for x in range(32,126)]) + '_'*130path=path.strip()path=path[17:] #to remove the file://localhost/ partpath=urllib.unquote(path)if path.translate(ASCII_TRANS) != path: # Contains non-ascii path = path.depre('utf8')path=urllib.url2pathname(path)


