Python DictWriter: OSError: [Errno 22] Invalid argument: '\u202aC:Users\\...\\Desktop\\file.csv'

Python DictWriter: OSError: [Errno 22] Invalid argument: '\u202aC:Users\\...\\Desktop\\file.csv'

我知道这个问题已经被问过和回答过几次了。尽管如此,到目前为止发布的解决方案并没有解决我的问题。 我在尝试使用 Python DictWriter.

打开 csv 文件时收到此错误代码

到目前为止,我至少尝试了以下变体:

with open(r"‪C:/Users/.../Desktop/file.csv", 'w') as xfile:
with open("‪C:\\Users\...\Desktop\file.csv", 'w') as xfile:
with open(r"‪C:\Users\...\Desktop\file.csv", 'w') as xfile:

Whosebug 请求的报价。 我也使用 append 方法而不是 write 没有效果。 三个点是占位符,路径完整正确。

那么这里可能是什么问题? 任何帮助将非常感激! 问候 托马斯

根据评论,解决方案是删除路径开头的不可见控制字符 U+202A(从左到右嵌入)。

This blog post from Raymond Chen 为该字符出现在 Windows 文件路径的开头提供了一种可能的解释。