0

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:

  1. Double the \ to escape the unicode of the "\U"

  2. Use a raw string filename = r"C:\Users.."

  3. 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

10
  • 1
    Are you trying 1 and 2 together or separately? And have you actually tried 3? Commented Dec 12, 2016 at 23:59
  • You aren't actually changing the path when switching from "\" to "/", its even the default format of pwd >>> os.system("pwd") /Users/omer/Dropbox/school/16_fall Commented Dec 13, 2016 at 0:05
  • I've tried every combination, but to answer your question I've mostly tried them separately. I'll post what the output is from what I am doing so you know what I'm seeing Commented Dec 13, 2016 at 0:08
  • 'C:\\Users\David\Desktop\mlb_stats_5AUG2016' returns '\\Users\\David\\Desktop\\mlb_stats_5AUG2016' Commented Dec 13, 2016 at 0:09
  • 1
    The doubled slash from options 1 & 2 is not wrong. It's just that you're viewing the repr of the string at the interactive console, which shows the escapes you'd need to recreate the string if you were typing it out. Try doing print(escaped_string), you won't see the escapes because print is showing you the human friendly version of the string, w/o the escapes. Commented Dec 13, 2016 at 0:18

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have a string with an escape character in it (\) and Python displays it's representation, 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.