Reputation: 3
I have been having a horrible time just trying to open a file in python 3 because of the "\U" in the "C:\Users..." microsoft path name. I'm getting the error "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes..."
I've read the other answers to this problem that offer the following solutions:
Double the \ to escape the unicode of the "\U"
Use a raw string filename = r"C:\Users.."
Switch the \ to /.
Whenever I use option 1 and/or 2 it doubles the "\". So, the filepath is wrong because it reads 'C:\\Users\\..." instead of 'C:\Users\...'
I'm trying to open excel files at work to manipulate data, and so option 3 isn't available to me, as I can't change the filepath.
Can someone explain why the "\" are doubling when I use the escape "\" or a raw string?
I've tried every combination of options and can't seem to get this to work.
Sorry for making a new question to an already answered problem, but I couldn't comment on the other answers, and the accepted answers weren't working for me.
Thanks
Upvotes: 0
Views: 3537
Reputation: 538
The original answers should work.
Option 1:
file_path = "c:\\User\\USER\\SOMETHINGELSE"
print(file_path);
gives:
c:\User\USER\SOMETHINGELSE
The slash escapes the character next to it, but doesn't print itself.
Option 2:
file_path = r"c:\User\USER\SOMETHINGELSE"
print(file_path);
gives:
c:\User\USER\SOMETHINGELSE
The r tells the string that it has to take it as literal and not use any escape characters.
Option 3:
OK...So if you really can't use options 1 or 2, you could use:
import os
file_path = os.path.join(os.path.abspath(os.sep), 'Users', 'USER', 'SOMETHINGELSE')
print(file_path);
In this case 'os.path.abspath(os.sep)' returns the root drive you are currently using. In my case C:\. 'os.path.join' concatenates the strings using the current systems delimiter. On windows this is \.
The result is:
C:\Users\USER\SOMETHINGELSE
But, that is a strange way of doing things when option 1 or 2 should work fine. Remember not to use the options together. If you combine options 1 and 2 you will not get the correct result. Use one or the other.
Upvotes: 2
Reputation: 26717
If you have a string with an escape character in it (\
) and Python displays it's repr
esentation, it will appear to be doubled:
# in the REPL
path = r'C:\Users\Nick'
path
# 'C:\\Users\\Nick'
print(path)
# C:\Users\Nick
print(repr(path))
# 'C:\\Users\\Nick'
You are likely being confused in the REPL by Python printing the representation of a string rather than what it actually contains.
Note that your choices 1 and 2 are identical:
'C:\\Users\\Nick' == r'C:\Users\Nick'
# True
Also note that if you enter an invalid escape (e.g. '\D'
), Python will silently correct this for you to '\\D'
. This will issue a DeprecationWarning
in Python 3.6 and a SyntaxError
at some point in the future.
Upvotes: 0