【发布时间】:2011-08-23 21:26:31
【问题描述】:
我有一个文件,路径如下: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'
更新 1:我在 Windows 7 上使用 Python 2.7
【问题讨论】:
-
那么,我该如何处理呢?
-
尝试使用 unicode 路径字符串:
path = path.decode('utf8'),在其余代码之前。 -
@Duncan - 试过了,还是不行。
-
@vr3690 您是否在 Windows 上?请您确认一下。
-
@Ignacio Vazquez-Abrams - 如何使用不同的编码?我应该在这里使用什么?
标签: python file path url-encoding