1

我正在使用 python 命令

os.chdir(base_dir)

更改目录。但是字符串 base_dir 包含 ASCII 范围之外的 utf-8 字符。Windows 资源管理器 (Windows 7) 上的目录列表以非 ASCII 字符显示目录名称,对应于路径 base_dir 。

但是,当我执行上述命令时,我得到了错误

WindowsError: [Error 2] The system cannot find the file specified: 'C:/Users/abhishek/Desktop/scripting/dir_struct/\xd0\x98\xd0\xbd\xd1\x84\xd0\xbe\xd1\x80\xd0\xbc\xd0\xb0\xd1\x82\xd0\xb8\xd0\xba\xd0\xb0'

有人可以告诉我如何在 python 中成功导航目录吗?

我的电脑的默认语言是英语。

4

1 回答 1

4

请改用 unicode 路径:

os.chdir(base_dir.decode('utf8'))

Windows 路径是 UTF-16 编码的,但 Python 知道如何正确处理 unicode 路径并将它们转换为适合您平台的正确文件系统编码。

于 2013-03-24T21:31:09.593 回答