删除 Python 中包含特殊字符的文件夹

Delete folders which contains special chars in Python

我有一些名称包含特殊字符的文件夹。 我试着像这样删除它们:

shutil.rmtree(os.path.join(mypath,"Input"))

我得到一个错误:

The filename, directory name, or volume label syntax is incorrect: ...\library elements ???? ?????

我的文件夹名称是:

library elements 階段要素 Элементы

如何删除这个文件夹?

谢谢大卫

假设您在 Python 2.x,这似乎是 known bug in Python 2.7。在 Windows 上使用 ANSI API 时,区域设置处理可能存在问题(Unicode 和区域设置处理在 2.x 行中弱得多)。

假设您无法切换到 Python 3.x,请尝试将路径作为 unicode 路径传递,而不是 str,因此它会执行 Windows Unicode API(将正确处理非 ASCII 名称)。由于您的基本路径似乎是 ASCII str,这可以通过更改来完成:

shutil.rmtree(os.path.join(mypath,"Input"))

至:

shutil.rmtree(os.path.join(mypath,"Input").decode('ascii'))