I beg-to-differ
Here is my complete code. Add a folder with a text called “Bake Beans Amd spam.txt”
The bottom TextCtrl you can add an regex. r’\sAnd\s’
You will see that the regex will not accept the first agument as a string!
Wow, this reads like a koan.
If you want to get a string from a wxTextCtrl, then using the .GetValue() method will, yes, in fact return a string. Then you can do whatever you want with that string.
I even used Tim Roberts suggestion and got the same results.
-- coding: utf-8 --
#!/usr/bin/env python
import os
import sys
import wx
import re
class MainFrame(wx.Frame):
def init(self):
args = sys.argv[1:]
wx.Frame.init(self,None,wx.ID_ANY,
title=‘List and Correct Naming Files’,
size=(600, 450))
self.filenames = None
self.hbox = wx.BoxSizer()
self.hbox2 = wx.BoxSizer()
self.hbox3 = wx.BoxSizer()
self.hbox4 = wx.BoxSizer()
self.vbox= wx.BoxSizer(wx.VERTICAL)
self.background = wx.Panel(self)
self.OpenDir = wx.TextCtrl(self.background,style=wx.PROCESS_ENTER)
self.OpenDir.Bind(wx.EVT_LEFT_UP,self.OnChooseRoot)
self.Detector = wx.TextCtrl(self.background,style=wx.PROCESS_ENTER)
self.BasicRemove = wx.TextCtrl(self.background,style=wx.PROCESS_ENTER)
self.BasicReplace = wx.TextCtrl(self.background,style=wx.PROCESS_ENTER)
self.AdvanceReplace = wx.TextCtrl(self.background,style=wx.PROCESS_ENTER)
self.middleview = wx.TextCtrl(self.background,style = wx.TE_MULTILINE|wx.TE_RICH)
self.bottomview = wx.TextCtrl(self.background,style = wx.TE_MULTILINE|wx.TE_RICH)
self.butExport = wx.Button(self.background,wx.ID_ANY,label = 'write to text')
self.butExport2 = wx.Button(self.background,wx.ID_ANY,label = 'rename')
self.butClear = wx.Button(self.background,wx.ID_ANY,label = 'clear')
self.Bind(wx.EVT_BUTTON, self.checkBtnClick, self.butExport)
self.Bind(wx.EVT_BUTTON, self.checkBtnClick2, self.butExport2)
self.Bind(wx.EVT_BUTTON, self.OnClear, self.butClear)
self.Bind(wx.EVT_TEXT_ENTER, self.TxtEnter, self.BasicRemove)
self.Bind(wx.EVT_TEXT_ENTER, self.TxtEnter, self.BasicReplace)
self.BasicRemove.Bind(wx.EVT_TEXT_ENTER, self.TxtEnter)
self.BasicReplace.Bind(wx.EVT_TEXT_ENTER, self.TxtEnter)
self.AdvanceReplace.Bind(wx.EVT_TEXT_ENTER, self.checkBtnClick2)
self.hbox.Add(self.OpenDir,proportion = 1, border=0)
self.hbox.Add(self.butExport,proportion = 0,border=0)
self.hbox.Add(self.butClear,proportion = 0,border=0)
self.hbox3.Add(self.BasicRemove,proportion = 1,border=0)
self.hbox3.Add(self.BasicReplace,proportion = 1,border=0)
self.hbox4.Add(self.AdvanceReplace,proportion = 0,border=0)
self.hbox2.Add(self.Detector,proportion = 3,border=0)
self.hbox2.Add(self.butExport2,proportion = 0,border=0)
self.vbox.Add(self.hbox,proportion = 0,flag = wx.EXPAND,border=0)
self.vbox.Add(self.middleview,proportion = 2,flag = wx.EXPAND,border=0)
self.vbox.Add(self.hbox2,proportion = 0,flag = wx.EXPAND,border=0)
self.vbox.Add(self.hbox3,proportion = 0,flag = wx.EXPAND,border=0)
self.vbox.Add(self.hbox4,proportion = 0,flag = wx.EXPAND,border=0)
self.vbox.Add(self.bottomview,proportion = 1,flag = wx.EXPAND,border=0)
self.background.SetSizer(self.vbox)
self.Show()
font1 = wx.Font(10, wx.SWISS, wx.ITALIC, wx.LIGHT, False, u'Courier New')
self.OpenDir.SetFont(font1)
self.OpenDir.SetForegroundColour((140,140,140))
self.OpenDir.SetValue("Click here to open directory")
font4 = wx.Font(10, wx.SWISS, wx.ITALIC, wx.LIGHT, False, u'Courier New')
self.Detector.SetFont(font4)
self.Detector.SetForegroundColour((140,140,140))
self.Detector.SetValue("commas in naming will be detected")
def OnChooseRoot(self, event):
self.middleview.Clear()
with wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE) as dlg:
if dlg.ShowModal() == wx.ID_OK:
for self.dirname, self.dirnames, self.filenames in os.walk(dlg.GetPath()):
for self.filename in self.filenames:
font1 = wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, u'Courier New')
self.middleview.SetFont(font1)
s=('\n')
bs=(os.path.join(self.filename,s))
bf = (bs.replace("\\",""))
self.middleview.AppendText(bf)
font3 = wx.Font(8, wx.MODERN, wx.NORMAL, wx.NORMAL, False, u'Courier New')
self.OpenDir.SetFont(font3)
self.OpenDir.SetFont(font3)
self.OpenDir.SetForegroundColour((0,0,0))
self.OpenDir.SetValue(self.dirname)
for self.dirname, self.dirnames, self.filenames in os.walk(dlg.GetPath()):
for self.filename in self.filenames:
if ',' in self.filename:
font4 = wx.Font(8, wx.SWISS, wx.ITALIC, wx.LIGHT, False, u'Courier New')
self.Detector.SetFont(font4)
self.Detector.SetForegroundColour((250,100,100))
self.Detector.SetValue("errors detected in naming")
font2 = wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False, u'Courier New')
self.bottomview.SetFont(font2)
s=('\n')
bs=(os.path.join(self.filename,s))
bf = (bs.replace("\\",""))
self.bottomview.AppendText(bf)
def checkBtnClick(self, bg):
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.txt", \
wx.SAVE | wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
bg = self.middleview.GetValue()
self.filename=dlg.GetFilename()
self.dirname=dlg.GetDirectory()
filehandle=open(os.path.join(self.dirname, self.filename),'w')
filehandle.write("FileList\n"+(bg))
v =("\\")
b= self.filename
self.OpenDir.SetValue(self.dirname + v + b)
font4 = wx.Font(8, wx.MODERN, wx.NORMAL, wx.NORMAL, False, u'Courier New')
self.OpenDir.SetFont(font4)
self.OpenDir.SetFont(font4)
self.OpenDir.SetForegroundColour((0,0,0))
self.butExport.SetLabel("written")
def OnClear(self, event):
self.middleview.Clear()
self.OpenDir.Clear()
self.OpenDir.SetValue("click here to load a folder again")
self.bottomview.Clear()
font4 = wx.Font(10, wx.SWISS, wx.ITALIC, wx.LIGHT, False, u'Courier New')
self.Detector.SetFont(font4)
self.Detector.SetForegroundColour((140,140,140))
self.Detector.SetValue("commas in naming will be detected")
self.BasicRemove.Clear()
self.BasicReplace.Clear()
self.BasicRemove.SetValue("string to remove")
self.BasicReplace.SetValue("string to replace")
def TxtEnter(self, event):
self.AdvanceReplace.GetValue()
self.BasicRemove.SetValue('')
event.Skip()
self.BasicRemove.GetValue()
self.BasicReplace.SetValue('')
event.Skip()
self.BasicReplace.GetValue()
def TxtEnter2(self, event):
pass
def checkBtnClick2(self, func):
if self.filenames:
path = self.dirname
dirList=os.listdir(path)
for self.filename in dirList:
f = self.filename
print self.AdvanceReplace.GetValue()
if not f.startswith(‘.’) and f.find(self.AdvanceReplace.GetValue()) >= 0:
print self.AdvanceReplace
print f
var = self.AdvanceReplace.GetValue()
print var, " print v"
s = re.sub(var,' & ',f)
print s, " print s"
os.rename(os.path.join(self.dirname, f), os.path.join(self.dirname,f.replace(self.BasicRemove.GetValue(),self.BasicReplace.GetValue())))
os.rename(os.path.join(self.dirname, f), os.path.join(self.dirname,s))
print f, " print f"
self.butExport2.SetLabel("renamed")
font4 = wx.Font(10, wx.SWISS, wx.ITALIC, wx.LIGHT, False, u'Courier New')
self.Detector.SetFont(font4)
self.Detector.SetForegroundColour((40,140,40))
self.Detector.SetValue("files are re-written, check directory")
self.bottomview.Clear()
self.middleview.Clear()
self.middleview.SetValue("click clear and load again")
if name == ‘main’:
import wx.lib.mixins.inspection as WIT
app = WIT.InspectableApp(redirect=True)
frame = MainFrame()
frame.Show()
app.MainLoop()
···
On Tuesday, August 20, 2013 9:19:18 PM UTC-5, Che M wrote: