1

Is there any way without typing r or 'double slash' to resolve this problem.because this two tricks change the type of file:

f = open('C:\Users\alireza\Desktop\exersices.python\p5.files\1.txt')

Error:

File "<ipython-input-1-243f8d6a931c>", line 1
    f = open('C:\Users\alireza\Desktop\exersices.python\p5.files\1.txt')
            ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
4
  • Can you please indent your code properly: stackoverflow.com/editing-help and please ask your question not only in the title, this might also be helpful: stackoverflow.com/help/how-to-ask. Commented Sep 9, 2018 at 9:56
  • You can use / forward slash to refer to the path of the file in windows. Commented Sep 9, 2018 at 10:30
  • 1
    I don't think using r'...' literals or double backslashes would change the type of the file in any way. What makes you think it does? Commented Sep 9, 2018 at 11:14
  • Why is this question tagged "unicode", I don't see anything outside ASCII? It does not take any knowledge about unicode to see that the substring "sers\ali" in not a valid hexadecimal number. Commented Sep 9, 2018 at 12:10

1 Answer 1

2

This will solve your issue.

f = open('C:/Users/alireza/Desktop/exersices.python/p5.files/1.txt')
Sign up to request clarification or add additional context in comments.

1 Comment

Note that if long-path support (i.e. more than 260 characters) is required via the "\\\\?\\" prefix, the path must be unicode (e.g. a u"" literal in Python 2) and use backslash as the path separator. Also if it's passed as a command-line argument, a path should use backslash since many programs blindly interpret forward slash as an option/switch. You can normalize forward slashes to backslashes via os.path.normpath.

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.