试图通过python创建一个快捷方式

时间:2014-02-19 05:12:15

标签: python python-2.7 py2exe

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()

使用py2exe它显示此错误并且不编译: SyntaxError:'unicodeescape'编解码器无法解码位置上的字节 2-3:截断\ UXXXXXXXX转义

1 个答案:

答案 0 :(得分:1)

您需要在以下字符串文字中转义\。否则,它被识别为unicode转义序列。

>>> "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

明确逃脱他们:

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

或使用原始字符串文字:

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

BTW,Python 2.x不会引发字符串文字"C:\Use..."的SyntaxError(除非你使用from __future__ import unicode_literals)。检查在使用py2exe时是否使用Python 3.x.