我有一个文件,路径如下:D:/bar/クレイジー・ヒッツ!/foo.abc
我正在解析来自 XML 文件的路径并将其存储在名为 path
的变量中,格式为 file://localhost/D:/bar/クレイジー・ヒッツ!/foo.abc
然后,正在执行以下操作:
path=path.strip()
path=path[17:] #to remove the file://localhost/ part
path=urllib.url2pathname(path)
path=urllib.unquote(path)
错误是:
IOError: [Errno 2] No such file or directory: 'D:\\bar\\\xe3\x82\xaf\xe3\x83\xac\xe3\x82\xa4\xe3\x82\xb8\xe3\x83\xbc\xe3\x83\xbb\xe3\x83\x92\xe3\x83\x83\xe3\x83\x84\xef\xbc\x81\\foo.abc'
我在 Windows 7 上使用 Python 2.7
原文由 bcosynot 发布,翻译遵循 CC BY-SA 4.0 许可协议
你的错误路径是:
我认为这是您的文件名的 UTF8 编码版本。
我在 Windows7 上创建了一个同名文件夹,并在其中放置了一个名为“abc.txt”的文件:
所以看来邓肯的建议
path.decode('utf8')
可以解决问题。更新
我无法为您测试这个,但我建议您在执行
.decode('utf8')
之前尝试检查路径是否包含非 ascii。这有点hacky …