0

I have a file I open, I then put the contents of this file into a list. I then split the list at "\r" and output this to a textctrl. the problem lies that my list.txt is 4 lines long, but when i open this in my program, it goes from 4 lines to 10, and duplicates some of the text. no idea where i'm going wrong.

example of my list.txt

A
B
C
D

what my program writes to the textctrl multiline box

A
A
B
A
B
C
A
B
C
D

I'm fairly new to python and wxpython, so to me, my code looks OK, and I cannot see where it's duplicating it.

 def OnOpen(self,e):
 dlg = wx.FileDialog(self, "Choose a file to open", self.dirname, "", "*.*", wx.OPEN) #open the dialog boxto open file
 if dlg.ShowModal() == wx.ID_OK: #if positive button selected....
 directory, filename = dlg.GetDirectory(), dlg.GetFilename()
 self.filePath = '/'.join((directory, filename)) 
 f = open(os.path.join(directory, filename), 'r') #traverse the file directory and find filename in the OS
 self.myList = []
 for line in f:
 self.myList.append(line)
 for i in (self.myList):
 for j in i.split("\r"):
 self.urlFld.AppendText(j)
 self.fileTxt.SetValue(self.filePath)
 f.close
 dlg.Destroy()
Torxed
23.6k15 gold badges91 silver badges135 bronze badges
asked Apr 16, 2013 at 7:51
3
  • 1
    To be quite honest, your coding style, naming conventions etc. are quite deviant. Google python coding style. Commented Apr 16, 2013 at 7:55
  • Heh, 3 nested for loops is perhaps not ideal.. and you should tag wxPython in your thread if that's what you're using to display the box. Commented Apr 16, 2013 at 8:02
  • f.close does not actually call the function. Commented Apr 16, 2013 at 9:48

2 Answers 2

1

wait, i got it, my indentation was wrong!! such a silly thing!

solved :)

new code:

def OnOpen(self,e):
dlg = wx.FileDialog(self, "Choose a file to open", self.dirname, "", "*.*", wx.OPEN) #open the dialog boxto open file
if dlg.ShowModal() == wx.ID_OK: #if positive button selected....
 directory, filename = dlg.GetDirectory(), dlg.GetFilename()
 self.filePath = '/'.join((directory, filename)) 
 f = open(os.path.join(directory, filename), 'r') #traverse the file directory and find filename in the OS
 self.myList = []
 for line in f:
 self.myList.append(line)
 for i in (self.myList):
 for j in i.split("\r"):
 self.urlFld.AppendText(j)
 self.fileTxt.SetValue(self.filePath)
 f.close
dlg.Destroy()
answered Apr 16, 2013 at 7:53
Sign up to request clarification or add additional context in comments.

1 Comment

Congratulations but you aren't actually closing the file btw. (A great way to avoid this, and have better style, is by using the with statement). Also why not iterate through the lines, adding to self.mylist and for j in i.split("\r"): self.urlFld.AppendText(j) at the same time?
0

Use 'with' to open the FileDialog then it will get destroyed when finished with.

Let the control load the file itself using the method 'LoadFile', then you dont need to worry about opening/closing the file yourself.

Use the control's method 'GetValue()' and split the result to create the list.

def OnOpen(self,e):
 with wx.FileDialog(self, "Choose a file to open", self.dirname,
 "", "*.*", wx.OPEN) as dlg:
 if dlg.ShowModal() == wx.ID_OK:
 directory, filename = dlg.GetDirectory(), dlg.GetFilename()
 self.filePath = '/'.join((directory, filename))
 self.urlFld.LoadFile(self.filePath)
 self.myList = self.urlFld.GetValue().split('\n')
answered Apr 16, 2013 at 12:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.