0

Here's my code.

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

The error given is this:

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

It's supposed to do the same thing as this:

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

but just write to a file in a different location. I want it to work for each different user, so I replaced my name with os.path.expanduser(path) as shown here. I'm almost certain I'm using os.path.expanduser(path) wrong, help? What am I supposed to do?

I've already created the file in Documents\joseDzirehChongToDoList\toDoListSaveFile.json by the way.

Community
  • 1
  • 1
  • Possible duplicate of [Why do I get a SyntaxError for a Unicode escape in my file path?](http://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path) – Chanda Korat Feb 10 '17 at 11:12
  • Possible duplicate of [Why do I get a SyntaxError for a Unicode escape in my file path?](http://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path) – Aran-Fey Feb 10 '17 at 11:52
  • 1
    You are using expanduser method wrong. Actually, you are not using that method at all. You are just trying to give a path named 'os.path.expanduser(path)' (as literal string, not a method call) – Lafexlos Feb 10 '17 at 14:29

2 Answers2

0

os.path.expanduser is a function, which means you can't just insert the literal string "os.path.expanduser(path)" inside a string and expect it to work.

The whole purpose of os.path.expanduser is to convert ~ to the users home directory. For example, `os.path.expanduser("~") might return "/Users/Josalina".

Also, if you use backward slashes in file paths, you need to either escape them in a normal string (eg: "foo\\bar"), or user a raw string which prevents the backslash from having special meaning (eg: r"foo\bar"). What many people don't seem to know is that forward slashes are supported by windows, so you can also use "foo/bar".

Finally, it's a good idea to save the filename to a variable before using it, so that it's easier to debug this problem. If you had put the variable in a filename, you could add a print statement (eg: print(filename)) to verify that the full filename is what you expected it to be.

Putting that all together, if you want to reference "Documents\joseDzirehChongToDoList\toDoListSaveFile.json" in your home directory, the best approach would be to do it like this:

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

Since forward slashes are valid on windows, you could also do it like this and remove the need for a raw string, with the added benefit that this code would also work unchanged on linux and osx:

filename = os.expanduser("~/Documents/joseDzirehChongToDoList/toDoListSaveFile.json")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
-1

The problem is, you haven't told the program what to do with the file. You're looking to read the file, so you should use:

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

The r is telling python what to do with the file. Other ways to open files are:

Using a w writes a file,

Using an A appends a file.

Hope I could help :)

Jake
  • 772
  • 2
  • 8
  • 18
  • this is literally looking for a folder named "os.path.expanduser(path)", _not_ the result of the command `os.path.expanduser(path)`. Also, you aren't properly handling the backslashes so you'll end up with exactly the same error. – Bryan Oakley Feb 10 '17 at 16:13
  • I don't believe you, unless you have a folder on your machine with the literal name "os.path.expanduser(path)" inside the folder "C:\users", – Bryan Oakley Feb 10 '17 at 16:16