Python: OSError: [Errno 22] Invalid argument: wrong path (on Output seems that Python modify my path )

Question:

hella.

seems to be a wrong path on my Python code. But I test again and again, the path and the file are good: c:Folder1bebe.txt

See the error OSError: [Errno 22] Invalid argument: 'c:\Folder1x08ebe.txt'

Python modify my path ??! Can you help me? Also, you have the entire code HERE:

enter image description here

Asked By: user15950808

||

Answers:

Your problem is most likely this line:

file_path = 'C:Somethingbooh'

In a Python string literal, backslashes are used to introduce special characters. For example n means a newline and b means a backspace. To actually get a backslash, you have to type \. A backslash followed by a character with no special meaning is left alone, so S actually means S (though relying on this is probably a bad idea).

You can either type your line like this

file_path = 'C:\Something\booh'

or use Pythons "raw string" syntax, which turns off the special meaning of backslashes, and type

file_path = r'C:Somethingbooh'

Notice that when you do

s = '\'

the string referred to by s actually contains a single backslash. For example, len(s) will be 1, and print(s) will print a single backslash.

Answered By: Ture Pålsson
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.