0

I have a program that is supposed to be comparing the contents of a list to values returned by a tinter treeview and if the values do not match, writing the element of the list to a file. The idea is to allow the user to remove an element in the tree (which is populated by reading from a the same file I'm trying to write to). here is the code:

    selected_book = info_box.focus()
    del_book = info_box.item(selected_book, 'values')
    title_file_clear = open("titles", 'w')
    author_file_clear = open("authors", 'w')
    title_file_clear.close()
    author_file_clear.close()
    title_file_3 = open("titles", "a")
    author_file_3 = open("authors", "a")
    for i in range(0,len(titles)):
        if titles[i] == del_book[0] is False:
            print(titles[i], file=title_file_3)
    for i in range(0,len(authors)):
        if authors[i] == del_book[1] is False:
            print(authors[i], file=author_file_3)
    title_file_3.close()
    author_file_3.close()

But all it seems to do is blank the files. (I do know this is not likely to be the most efficient piece of code, but I've been tweaking it for a while to try to get it to work)

  • 1
    Why not `if titles[i] != del_book[0]` instead of `is False`? – Michał Mar 21 '14 at 05:15
  • You open the files, but never read from the files. Are you sure this is working code ? What is `titles` ? and what is `authors` ? One recommendation : execute the program, and make sure there are no syntax errors, so we can help you with the logical errors. – karthikr Mar 21 '14 at 05:16
  • That's how `print` works. – Michał Mar 21 '14 at 05:21
  • @karthikr Yeah, this is just one module in the code, the rest works fine. I just assumed it wasn't necessary to upload all 190+ lines. – user3430747 Mar 21 '14 at 05:35

1 Answers1

0

Use if titles[i] != del_book[0]: instead of if titles[i] == del_book[0] is False:. To append file instead of writing new lines take a look at this question.

Community
  • 1
  • 1
Michał
  • 2,456
  • 4
  • 26
  • 33