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:\Folder1\bebe.txt
See the error OSError: [Errno 22] Invalid argument: 'c:\\Folder1\x08ebe.txt'
Python modify my path ??! Can you help me? Also, you have the entire code
CodePudding user response:
(Please don't post images of code; post the actual code, properly formatted, instead. That makes it much easier to read!)
That said, your problem is most likely this line:
file_path = 'C:\Something\booh'
(I didn't get that exactly right, because I can't copy-paste from a screenshot!)
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:\Something\booh'
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.
CodePudding user response:
Python technically didn't modify your path. The true cause is that the filepath needs to be properly escaped. What that means is to replace each single backslash with two, or to use the pathlib
module [1].
What happened was that Python interpreted the \b
in your file_path as the ASCII backspace. [2] You can see this in action by doing: print('\b')
which will produce the same output in the reported error.
Reference: