سؤال

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)

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top