I’ve appended a directory dialog to a TextCtrl. Only problem is the ‘\n’ includes the backslash.
Output:
a.txt
b.txt
c.txt
d.txt
Using the syntax [:-1] is only extracting the file string. Using .replace(r’\n’,’\n’) has no effect. Perhaps I’m not
implementing this right. Never-the-less I’m still trying to omit the backslash. My code reads:
def OnChooseRoot(self, event):
dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
for dirname, dirnames, filenames in os.walk(dlg.GetPath()):
for filename in filenames:
font1 = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL, False, u'Courier New')
self.middleview.SetFont(font1)
s=('\n')
bs=(os.path.join(filename,s))
self.middleview.AppendText(bs)
self.bottomview.AppendText(bs)
print os.path.join(filename)
dlg.Destroy()
From Robin’s quote, do I need to convert to raw strings to do this?
I’m not sure on how to go about this.
Robin:
The backslash magic happens only in string literals. In other words,
when you have a (non-raw) string constant in the source code. In that
case the ‘\n’ is written into the string data as the newline character
instead of a backslash and an ‘n’.
On the other hand, when you have a string value in sys.argv[1] or any
other variable it is just data. If that data happens to have a
backslash and a ‘n’ next to each other then that is all it is. Just a
backslash and a ‘n’. If you replace those two characters with a newline
character then the string has a newline in it. A newline character is
written as a string literal like ‘\n’ or as chr(10).