Search code examples
python-2.7loopswxpythonwritetofile

Write wxListBox to .txt file in Python


I have a wxListBox filled with strings. I would like to write the contents of my wxListBox to a .txt file.

I have tried:

def saveDB(self, parent):

    listBox = ""
    for i in range(self.listBox.GetCount()):
        listBox = self.listBox.GetString(i) + "\n"

This does not seem to be getting the job done. How can I write the contents to a .txt file?


Solution

  • listBox = ""
        for i in range(self.listBox.GetCount()):
            listBox = self.listBox.GetString(i) + "\n"
    

    This just got the listbox content into a 'listBox' variable.

    You need to write to a file, something like this should do.

    f = open('yourfile.txt', 'w')
    f.write(listBox)
    f.close()