Search code examples
pythonexport-to-csv

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


I know this has been asked and answered a couple of times. Still, the solutions posted so far do not fix my problem. I'm getting this error code when trying to open a csv-file with Python DictWriter.

I tried at least the following variations so far:

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:

Quotes requested by stackoverflow. I also used append method instead of write with no effect. The three dorts are a placeholder, the path is complete and correct.

So what could be the problem here? Any help would be much appreciated! Regards Thomas


Solution

  • From the comments, the solution was to delete the invisible control character U+202A (Left-to-Right Embedding) at the start of the path.

    This blog post from Raymond Chen provides one possible explanation for this character turning up at the start of a Windows file path.