eda
eda

Reputation: 55

OSError: [Errno 22] Invalid argument, opening a file in python

I want to open a file and although I have given its address correctly, an error appears when I run the program.

This is my code:

file1 = open('‪‪C:\pronouns.txt‪', 'r')

This is the error:

OSError: [Errno 22] Invalid argument: '\u202a\u202aC:\\pronouns.txt\u202a'

Upvotes: 0

Views: 4330

Answers (3)

Kevin li
Kevin li

Reputation: 21

I faced the same issue when I tried to copy filename directly from win10 file security properties dialog.

Here's Why is there an invisible U+202A at the start of my file name? helped me out, and maybe helpful to you as well.

The mysterious Unicode character "\u202a" is a formatting control character that means "LEFT-TO-RIGHT EMBEDDING" which is used to force text to be interpreted as left-to-right. However, it's invisible, if you try to copy the text out of the dialog box, the Unicode formatting control character comes along for a ride and may create all sorts of silent confusion.So, just input file path manually.

Upvotes: 2

Aris Budianto
Aris Budianto

Reputation: 11

file1 = open('‪‪C:\pronouns.txt‪', 'r').

rename the first character your file with capital

file1 = open('‪‪C:\Pronouns.txt‪', 'r')

Upvotes: 0

Pavan Kumar Polavarapu
Pavan Kumar Polavarapu

Reputation: 460

Forward and Backward slashes are always tricky. Can you try

file1 = open('‪‪C:/pronouns.txt‪', 'r')

Upvotes: 0

Related Questions