1

Python 2.7

from Tkinter import *
import os

class App:
    def __init__(self, master):
        self.frame = Frame(master)
        self.b = Button(self.frame, text = 'Open', command = self.openFile)
        self.b.grid(row = 1)
        self.frame.grid()
    def openFile(self):
        os.startfile("C:\Users\David\Desktop\minecraft.jar")

root = Tk()
app = App(root)
root.mainloop()

using py2exe it shows this error and its not compiling: SyntaxError: 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • related: [“Unicode Error ”unicodeescape" codec can't decode bytes… Cannot open text files in Python 3](http://stackoverflow.com/q/1347791/4279) – jfs Feb 19 '14 at 08:52

1 Answers1

1

You need to escape \ in the following string literal. Otherwise, it is recognized as unicode escape sequence.

>>> "C:\Users\David\Desktop\minecraft.jar"
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Explicitly escape them:

>>> "C:\\Users\\David\\Desktop\\minecraft.jar"
'C:\\Users\\David\\Desktop\\minecraft.jar'

or use raw string literals:

>>> r"C:\Users\David\Desktop\minecraft.jar"
'C:\\Users\\David\\Desktop\\minecraft.jar'

BTW, Python 2.x does not raise the SyntaxError for the string literal "C:\Use..." (unless you use from __future__ import unicode_literals). Check if you're using Python 3.x when using py2exe.

falsetru
  • 357,413
  • 63
  • 732
  • 636