2017-02-10 43 views
0

這是我的代碼。Python拒絕打開文件,給'unicodeescape'錯誤(與os.path.expanduser(路徑)問題)

# -*- coding: utf-8 -*- 
""" 
Created on Sun Jan 22 14:47:36 2017 

@author: Jose Chong 
""" 
import json 
import tkinter 

master = tkinter.Tk() 
master.title("To-Do List (with saving!)") 
master.geometry("300x300") 

masterFrame = tkinter.Frame(master) 

masterFrame.pack(fill=tkinter.X) 

checkboxArea = tkinter.Frame(masterFrame, height=26) 

checkboxArea.pack(fill=tkinter.X) 

inputStuff = tkinter.Frame(masterFrame) 

checkboxList = [] 

with open('C:\Users\os.path.expanduser(path)\Documents\joseDzirehChongToDoList\toDoListSaveFile.json') as infile:  
    checkboxList = json.load(infile) 

def destroyCheckbox(checkbox, row): 
    row.destroy() 
    del checkboxList [checkboxList.index(str(checkbox))] 

    with open('C:\Users\os.path.expanduser(path)\Documents\joseDzirehChongToDoList\toDoListSaveFile.json', 'w') as outfile: 
     json.dump(checkboxList, outfile) 

for savedCheckbox in checkboxList: 
    checkboxRow = tkinter.Frame(checkboxArea) 
    checkboxRow.pack(fill=tkinter.X) 
    checkbox1 = tkinter.Checkbutton(checkboxRow, text=savedCheckbox) 
    checkbox1.pack(side=tkinter.LEFT) 
    deleteItem = tkinter.Button(checkboxRow, text="x", bg="red", fg="white", 
           activebackground="white", activeforeground="red", 
           command=lambda c=savedCheckbox, r=checkboxRow: destroyCheckbox(c, r)) 
    deleteItem.pack(side=tkinter.RIGHT) 

    with open('C:\Users\os.path.expanduser(path)\Documents\joseDzirehChongToDoList\toDoListSaveFile.json', 'w') as outfile: 
     json.dump(checkboxList, outfile) 

def drawCheckbox(): 
    newCheckboxInput = entry.get() 
    def destroyCheckbox(): 
     checkboxRow.destroy() 
     del checkboxList[checkboxList.index(newCheckboxInput)] 
     with open('C:\Users\os.path.expanduser(path)\Documents\joseDzirehChongToDoList\toDoListSaveFile.json', 'w') as outfile: 
      json.dump(checkboxList, outfile)     
    checkboxList.append(newCheckboxInput) 
    entry.delete(0,tkinter.END) 
    checkboxRow = tkinter.Frame(checkboxArea) 
    checkboxRow.pack(fill=tkinter.X) 
    checkbox1 = tkinter.Checkbutton(checkboxRow, text = checkboxList[-1]) 
    checkbox1.pack(side=tkinter.LEFT) 
    deleteItem = tkinter.Button(checkboxRow, text = "x", command=lambda c=savedCheckbox, r=checkboxRow: destroyCheckbox(c, r), bg="red", fg="white", activebackground="white", activeforeground="red") 
    deleteItem.pack(side=tkinter.RIGHT) 

    with open('C:\Users\os.path.expanduser(path)\Documents\joseDzirehChongToDoList\toDoListSaveFile.json', 'w') as outfile: 
     json.dump(checkboxList, outfile) 


def createInputStuff(): 
    paddingFrame = tkinter.Frame(inputStuff, height=5) 
    paddingFrame.pack(fill=tkinter.X) 
    buttonDone.pack() 
    inputStuff.pack() 
    buttonAdd.pack_forget() 
    master.bind('<Return>', lambda event: drawCheckbox()) 

def removeInputStuff(): 
    inputStuff.pack_forget() 
    buttonAdd.pack() 
    buttonDone.pack_forget() 
    master.unbind('<Return>') 


buttonDone = tkinter.Button(inputStuff, text = "Close Input", command=removeInputStuff) 


buttonAdd = tkinter.Button(masterFrame, text="Add Item", command=createInputStuff) 
buttonAdd.pack() 


topInput = tkinter.Frame(inputStuff) 
bottomInput = tkinter.Frame(inputStuff) 

topInput.pack() 
bottomInput.pack() 

prompt = tkinter.Label(topInput, text="What do you want your checkbox to be for?") 
prompt.pack() 
entry = tkinter.Entry(bottomInput, bd=3) 
entry.pack(side=tkinter.LEFT) 
buttonConfirm = tkinter.Button(bottomInput, text="Confirm", command=drawCheckbox) 
buttonConfirm.pack(side=tkinter.LEFT) 

master.mainloop() 

給出的錯誤是這樣的:

runfile('C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile/toDoList.py', wdir='C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile') 
    File "C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile/toDoList.py", line 26 
    with open('C:\Users\os.path.expanduser(path)\Documents\joseDzirehChongToDoList\toDoListSaveFile.json') as infile: 
      ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape 

它應該做同樣的事情,因爲這:

# -*- coding: utf-8 -*- 
""" 
Created on Sun Jan 22 14:47:36 2017 

@author: Jose Chong 
""" 
import json 
import tkinter 

master = tkinter.Tk() 
master.title("To-Do List (with saving!)") 
master.geometry("300x300") 

masterFrame = tkinter.Frame(master) 

masterFrame.pack(fill=tkinter.X) 

checkboxArea = tkinter.Frame(masterFrame, height=26) 

checkboxArea.pack(fill=tkinter.X) 

inputStuff = tkinter.Frame(masterFrame) 

checkboxList = [] 

with open('toDoListSaveFile.json') as infile:  
    checkboxList = json.load(infile) 

def destroyCheckbox(checkbox, row): 
    row.destroy() 
    del checkboxList [checkboxList.index(str(checkbox))] 

    with open("toDoListSaveFile.json", 'w') as outfile: 
     json.dump(checkboxList, outfile) 

for savedCheckbox in checkboxList: 
    checkboxRow = tkinter.Frame(checkboxArea) 
    checkboxRow.pack(fill=tkinter.X) 
    checkbox1 = tkinter.Checkbutton(checkboxRow, text=savedCheckbox) 
    checkbox1.pack(side=tkinter.LEFT) 
    deleteItem = tkinter.Button(checkboxRow, text="x", bg="red", fg="white", 
           activebackground="white", activeforeground="red", 
           command=lambda c=savedCheckbox, r=checkboxRow: destroyCheckbox(c, r)) 
    deleteItem.pack(side=tkinter.RIGHT) 

    with open("toDoListSaveFile.json", 'w') as outfile: 
     json.dump(checkboxList, outfile) 

def drawCheckbox(): 
    newCheckboxInput = entry.get() 
    def destroyCheckbox(): 
     checkboxRow.destroy() 
     del checkboxList[checkboxList.index(newCheckboxInput)] 
     with open("toDoListSaveFile.json", 'w') as outfile: 
      json.dump(checkboxList, outfile)     
    checkboxList.append(newCheckboxInput) 
    entry.delete(0,tkinter.END) 
    checkboxRow = tkinter.Frame(checkboxArea) 
    checkboxRow.pack(fill=tkinter.X) 
    checkbox1 = tkinter.Checkbutton(checkboxRow, text = checkboxList[-1]) 
    checkbox1.pack(side=tkinter.LEFT) 
    deleteItem = tkinter.Button(checkboxRow, text = "x", command=lambda c=savedCheckbox, r=checkboxRow: destroyCheckbox(c, r), bg="red", fg="white", activebackground="white", activeforeground="red") 
    deleteItem.pack(side=tkinter.RIGHT) 

    with open("toDoListSaveFile.json", 'w') as outfile: 
     json.dump(checkboxList, outfile) 


def createInputStuff(): 
    paddingFrame = tkinter.Frame(inputStuff, height=5) 
    paddingFrame.pack(fill=tkinter.X) 
    buttonDone.pack() 
    inputStuff.pack() 
    buttonAdd.pack_forget() 
    master.bind('<Return>', lambda event: drawCheckbox()) 

def removeInputStuff(): 
    inputStuff.pack_forget() 
    buttonAdd.pack() 
    buttonDone.pack_forget() 
    master.unbind('<Return>') 


buttonDone = tkinter.Button(inputStuff, text = "Close Input", command=removeInputStuff) 


buttonAdd = tkinter.Button(masterFrame, text="Add Item", command=createInputStuff) 
buttonAdd.pack() 


topInput = tkinter.Frame(inputStuff) 
bottomInput = tkinter.Frame(inputStuff) 

topInput.pack() 
bottomInput.pack() 

prompt = tkinter.Label(topInput, text="What do you want your checkbox to be for?") 
prompt.pack() 
entry = tkinter.Entry(bottomInput, bd=3) 
entry.pack(side=tkinter.LEFT) 
buttonConfirm = tkinter.Button(bottomInput, text="Confirm", command=drawCheckbox) 
buttonConfirm.pack(side=tkinter.LEFT) 

master.mainloop() 

只是寫在不同位置的文件。我希望它適用於每個不同的用戶,因此我用os.path.expanduser(path)替換了我的名字,如here所示。我幾乎肯定我使用os.path.expanduser(路徑)錯誤,幫助?我應該做些什麼?

我已經在Documents \ joseDzirehChongToDoList \ toDoListSaveFile.json中創建了該文件。

+0

的可能的複製[?爲什麼我會在我的文件路徑中的Unicode轉義一個SyntaxError(http://stackoverflow.com/問題/ 18084554/why-do-i-get -a-syntaxerror-for-a-unicode-escape-in​​-my-file-path) –

+0

可能的重複[爲什麼我的文件中出現Unicode轉義的SyntaxError ?路徑(HTTP:// ST ackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in​​-my-file-path) –

+1

您正在使用擴展名方法錯誤。其實,你根本沒有使用這種方法。你只是想給一個名爲'os.path.expanduser(path)'的路徑(作爲文字字符串,而不是方法調用) – Lafexlos

回答

0

os.path.expanduser功能,這意味着你不能只是插入文本字符串「os.path.expanduser(路徑)」在字符串中,並期望它的工作。

os.path.expanduser的全部用途是將~轉換爲用戶主目錄。例如,`os.path.expanduser(「〜」)可能會返回「/ Users/Josalina」。另外,如果在文件路徑中使用反斜槓,則需要以普通字符串(例如:"foo\\bar")或用戶原始字符串(防止反斜槓具有特殊含義)(例如:r"foo\bar")轉義它們。許多人似乎並不知道正斜線由Windows支持,因此您也可以使用"foo/bar"

最後,在使用它之前將文件名保存到一個變量是一個好主意,以便更容易調試此問題。如果您已將變量放在文件名中,則可以添加一個打印語句(例如:print(filename))以驗證完整文件名是您所期望的。

把這一切放在一起,如果你想引用「Documents \ joseDzirehChongToDoList \ toDoListSaveFile。JSON」在你的home目錄下,最好的辦法是做這樣的:

filename = os.expanduser(r"~\Documents\joseDzirehChongToDoList\toDoListSaveFile.json") 
with open(filename, "w") as outfile: 
    ... 

由於正斜槓是在Windows上有效的,你也可以像這樣做並刪除不需要原始字符串,額外的好處,這一代碼也將工作在Linux和OSX不變:

filename = os.expanduser("~/Documents/joseDzirehChongToDoList/toDoListSaveFile.json") 
-1

問題是,您還沒有告訴程序如何處理文件。您正在尋找讀取這個文件,所以你應該使用:

with open(r'C:\Users\os.path.expanduser(path)\Documents\joseDzirehChongToDoList\toDoListSaveFile.json', 'w') 

r告訴蟒蛇做什麼用的文件。 打開文件的其他途徑有:

使用w寫入文件,

使用的A附加的文件。

希望我能幫助:)

+0

這實際上是尋找名爲「os.path.expanduser(path)」的文件夾,_not_命令'os.path.expanduser(path)'的結果。另外,你沒有正確處理反斜槓,所以你最終會得到完全相同的錯誤。 –

+0

這對我有效... – Jake

+0

我不相信你,除非你的機器上有一個文件夾,文件夾「C:\ users」中的文字名爲「os.path.expanduser(path)」, –