Adding regex to os.rename, os.path.join

I have this widget that works fine for replacing filenames however I’m not getting any results
with adding regex. (self.dirname,f.re.split) I comment out the re.split. I need to know if this is strictly a python
question or perhaps this needs another function to work.

-- 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.TxtEnter)
   
    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.BasicRemove.GetValue()) >= 0:
                                print self.AdvanceReplace.GetValue()
                                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,f.re.split(self.AdvanceReplace.GetValue())))

                                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()

···

I have this widget that works fine for replacing filenames however I’m not getting any results
with adding regex. (self.dirname,f.re.split) I comment out the re.split. I need to know if this is strictly a python

question

Well yes, this is a Python, rather that wxPython–not that you won’t find help here.

But both for you debugging and testing, and to facilitate asking questions, you should pull out the regex stuff into a stand alone function and write some tests against it. Isolating the issue will likely make it much clearer.

Chris

···

On Aug 19, 2013, at 5:57 PM, George McCown georgemccown@gmail.com wrote:

or perhaps this needs another function to work.

-- 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.TxtEnter)

   
    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.BasicRemove.GetValue()) >= 0:

                                print self.AdvanceReplace.GetValue()
                                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,f.re.split(self.AdvanceReplace.GetValue())))

                                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()

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

I’m passing a string when I need a varible. How can you set a TextCtrl box to hold varibles? Do you have an example?

···

On Tuesday, August 20, 2013 9:43:16 AM UTC-5, Chris Barker - NOAA Federal wrote:

On Aug 19, 2013, at 5:57 PM, George McCown george...@gmail.com wrote:

I have this widget that works fine for replacing filenames however I’m not getting any results
with adding regex. (self.dirname,f.re.split) I comment out the re.split. I need to know if this is strictly a python

question

Well yes, this is a Python, rather that wxPython–not that you won’t find help here.

But both for you debugging and testing, and to facilitate asking questions, you should pull out the regex stuff into a stand alone function and write some tests against it. Isolating the issue will likely make it much clearer.

Chris

or perhaps this needs another function to work.

-- 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.TxtEnter)

   
    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.BasicRemove.GetValue()) >= 0:

                                print self.AdvanceReplace.GetValue()
                                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,f.re.split(self.AdvanceReplace.GetValue())))

                                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()

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

George McCown wrote:

I have this widget that works fine for replacing filenames however I'm
not getting any results
with adding regex. (self.dirname,f.re.split) I comment out the
re.split. I need to know if this is strictly a python
question or perhaps this needs another function to work.

Your question doesn't make sense, and neither does your code.
"f.replace" works because "replace" is a member function for strings.
"f.re.split" doesn't work because "re" is not a member for strings.
"re" is a standalone module. Even if it were, re.split is not what you
want.

Step back. Think about what you're trying to do, step by step. Write a
couple of 10-line Python scripts so you can test whether these functions
work the way you think they do. Then, once you understand the API, you
can integrate them into your larger script.

I suspect that you are trying to change your find and replace operation
from using simple text substitution to using regular expressions. In
that case, the usual replacement for string.replace would be re.sub.
Instead of
    f.replace( oldtext, newtext )
you need
    re.sub( regex, newtext, f )
NOTE here that re.sub is a standalone function. That means you have to
pass it the target string "f". Also note that the regex must be a
regular expression.

Now, go play with some sample strings in a command line and make sure
you understand how they work.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

George McCown wrote:

I'm passing a string when I need a varible.

No, you aren't.

How can you set a TextCtrl box to hold varibles?

It doesn't. A TextCtrl IS a variable. That variable holds a string.

What you are doing is not programming, it's hacking. Please, learn a
little discipline. Sit down with a piece of paper and write out in
words ("pseudocode") what you want to do at each step. Figure out what
information you HAVE, and what information you NEED. Right now, your
code is a confused mess.

Your "rename" button handler is called "checkBtnClick2". Surely that
doesn't make sense to you. The first line in that function checks
whether "self.filenames" is empty, but "self.filenames" is just an
intermediate variable in the earlier functions. Further, self.filenames
is not used in checkBtnClick2, so why do you care if it is empty?

Also, you have your "AdvanceReplace" as the replacement part, but for a
regular expression, you want it to be the "Remove" part. I've made that
change here. If you fill in BasicRemove and BasicReplace, it will do a
simple text substitution. If you fill in AdvanceRemove and
BasicReplace, it will do a regex substitution.

Look, here is a program that does what I think you want. Hopefully, you
can look through this code and figure out what I changed, and why I
changed it.

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os
import wx
import re

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,wx.ID_ANY,
                          title='List and Correct Naming Files',
                          size=(600, 450))
        self.dirname = None
      
        hbox = wx.BoxSizer()
        hbox2 = wx.BoxSizer()
        hbox3 = wx.BoxSizer()
        hbox4 = wx.BoxSizer()
        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.AdvanceRemove =
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.butRename = 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.onBtnWrite, self.butExport)
        self.Bind(wx.EVT_BUTTON, self.onBtnRename, self.butRename)
        self.Bind(wx.EVT_BUTTON, self.OnClear, self.butClear)
      
        hbox.Add(self.OpenDir,proportion = 1, border=0)
        hbox.Add(self.butExport,proportion = 0,border=0)
        hbox.Add(self.butClear,proportion = 0,border=0)

        hbox3.Add(self.BasicRemove,proportion = 1,border=0)
        hbox3.Add(self.BasicReplace,proportion = 1,border=0)
        hbox4.Add(self.AdvanceRemove,proportion = 0,border=0)
      
        hbox2.Add(self.Detector,proportion = 3,border=0)
        hbox2.Add(self.butRename,proportion = 0,border=0)
      
        vbox.Add(hbox,proportion = 0,flag = wx.EXPAND,border=0)
        vbox.Add(self.middleview,proportion = 2,flag = wx.EXPAND,border=0)
        vbox.Add(hbox2,proportion = 0,flag = wx.EXPAND,border=0)
        vbox.Add(hbox3,proportion = 0,flag = wx.EXPAND,border=0)
        vbox.Add(hbox4,proportion = 0,flag = wx.EXPAND,border=0)
        vbox.Add(self.bottomview,proportion = 1,flag = wx.EXPAND,border=0)

        self.background.SetSizer(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()
        dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE)
        self.dirname = None
        if dlg.ShowModal() != wx.ID_OK:
            dlg.Destroy()
            return
        dlg.Destroy()

        self.dirname = dlg.GetPath()

        font1 = wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
u'Courier New')
        font2 = wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
u'Courier New')
        font3 = wx.Font(8, wx.MODERN, wx.NORMAL, wx.NORMAL, False,
u'Courier New')
        font4 = wx.Font(8, wx.SWISS, wx.ITALIC, wx.LIGHT, False,
u'Courier New')

        self.OpenDir.SetFont(font3)
        self.OpenDir.SetForegroundColour((0,0,0))
        self.OpenDir.SetValue(self.dirname)

        self.middleview.SetFont(font1)
        self.bottomview.SetFont(font2)
        self.Detector.SetFont(font4)

        for dirname, dirnames, filenames in os.walk(self.dirname):
            for filename in filenames:
                bs = filename.replace('\\','')+'\n'
                self.middleview.AppendText(bs)
                if ',' in filename:
                    self.Detector.SetForegroundColour((250,100,100))
                    self.Detector.SetValue("errors detected in naming")
                    self.bottomview.AppendText(bs)
        dlg.Destroy()
                      
    def onBtnWrite(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()
            filename=dlg.GePath()
            with open(filename,'w') as filehandle:
                filehandle.write("FileList\n"+(bg))
            self.OpenDir.SetValue(filename)
        dlg.Destroy()
        self.butExport.SetLabel("written")
      
    def OnClear(self, event):
        self.middleview.Clear()
        self.OpenDir.SetValue("click here to load a folder again")
        self.bottomview.Clear()
        self.Detector.SetForegroundColour((140,140,140))
        self.Detector.SetValue("commas in naming will be detected")
        self.BasicRemove.SetValue("string to remove")
        self.BasicReplace.SetValue("string to replace")

    def onBtnRename(self, func):
        if not self.dirname:
            return

        path = self.dirname
        dirList=os.listdir(path)
        oldr = self.AdvanceRemove.GetValue()
        oldv = self.BasicRemove.GetValue()
        newv = self.BasicReplace.GetValue()
        for self.filename in dirList:
            f = self.filename
            if f.find(oldv) >= 0:
                if oldr:
                    nn = re.sub(oldr, newv, f)
                else:
                    nn = f.replace(oldv, newv)
                on = os.path.join(self.dirname, f)
                nn = os.path.join(self.dirname, nn)
                print "rename", on, nn
                os.rename(on, nn)
        self.butRename.SetLabel("renamed")
        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=False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Tim Roberts wrote:

Look, here is a program that does what I think you want. Hopefully, you
can look through this code and figure out what I changed, and why I
changed it.

I didn't notice until I saw my own post that your first two function
were using os.walk, but the rename function was using os.listdir. Those
are very different. Here is another version that uses os.walk in all cases:

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os
import wx
import re

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,wx.ID_ANY,
                          title='List and Correct Naming Files',
                          size=(600, 450))
        self.dirname = None
      
        hbox = wx.BoxSizer()
        hbox2 = wx.BoxSizer()
        hbox3 = wx.BoxSizer()
        hbox4 = wx.BoxSizer()
        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.AdvanceRemove =
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.butRename = 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.onBtnWrite, self.butExport)
        self.Bind(wx.EVT_BUTTON, self.onBtnRename, self.butRename)
        self.Bind(wx.EVT_BUTTON, self.OnClear, self.butClear)
      
        hbox.Add(self.OpenDir,proportion = 1, border=0)
        hbox.Add(self.butExport,proportion = 0,border=0)
        hbox.Add(self.butClear,proportion = 0,border=0)

        hbox3.Add(self.BasicRemove,proportion = 1,border=0)
        hbox3.Add(self.BasicReplace,proportion = 1,border=0)
        hbox4.Add(self.AdvanceRemove,proportion = 0,border=0)
      
        hbox2.Add(self.Detector,proportion = 3,border=0)
        hbox2.Add(self.butRename,proportion = 0,border=0)
      
        vbox.Add(hbox,proportion = 0,flag = wx.EXPAND,border=0)
        vbox.Add(self.middleview,proportion = 2,flag = wx.EXPAND,border=0)
        vbox.Add(hbox2,proportion = 0,flag = wx.EXPAND,border=0)
        vbox.Add(hbox3,proportion = 0,flag = wx.EXPAND,border=0)
        vbox.Add(hbox4,proportion = 0,flag = wx.EXPAND,border=0)
        vbox.Add(self.bottomview,proportion = 1,flag = wx.EXPAND,border=0)

        self.background.SetSizer(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()
        dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE)
        self.dirname = None
        if dlg.ShowModal() != wx.ID_OK:
            dlg.Destroy()
            return
        dlg.Destroy()

        self.dirname = dlg.GetPath()

        font1 = wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
u'Courier New')
        font2 = wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
u'Courier New')
        font3 = wx.Font(8, wx.MODERN, wx.NORMAL, wx.NORMAL, False,
u'Courier New')
        font4 = wx.Font(8, wx.SWISS, wx.ITALIC, wx.LIGHT, False,
u'Courier New')

        self.OpenDir.SetFont(font3)
        self.OpenDir.SetForegroundColour((0,0,0))
        self.OpenDir.SetValue(self.dirname)

        self.middleview.SetFont(font1)
        self.bottomview.SetFont(font2)
        self.Detector.SetFont(font4)

        for dirname, dirnames, filenames in os.walk(self.dirname):
            for filename in filenames:
                bs = filename.replace('\\','')+'\n'
                self.middleview.AppendText(bs)
                if ',' in filename:
                    self.Detector.SetForegroundColour((250,100,100))
                    self.Detector.SetValue("errors detected in naming")
                    self.bottomview.AppendText(bs)
        dlg.Destroy()
                      
    def onBtnWrite(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()
            filename=dlg.GePath()
            with open(filename,'w') as filehandle:
                filehandle.write("FileList\n"+(bg))
            self.OpenDir.SetValue(filename)
        dlg.Destroy()
        self.butExport.SetLabel("written")
      
    def OnClear(self, event):
        self.middleview.Clear()
        self.OpenDir.SetValue("click here to load a folder again")
        self.bottomview.Clear()
        self.Detector.SetForegroundColour((140,140,140))
        self.Detector.SetValue("commas in naming will be detected")
        self.BasicRemove.SetValue("string to remove")
        self.BasicReplace.SetValue("string to replace")

    def onBtnRename(self, func):
        if not self.dirname:
            return

        oldr = self.AdvanceRemove.GetValue()
        oldv = self.BasicRemove.GetValue()
        newv = self.BasicReplace.GetValue()
        for dirname, dirnames, filenames in os.walk(self.dirname):
            for filename in filenames:
                if filename.find(oldv) >= 0:
                    if oldr:
                        nn = re.sub(oldr, newv, filename)
                    else:
                        nn = filename.replace(oldv, newv)
                    on = os.path.join(dirname, filename)
                    nn = os.path.join(dirname, nn)
                    print "rename", on, nn
                os.rename(on, nn)
        self.butRename.SetLabel("renamed")
        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=False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

I’m passing a string when I need a varible. How can you set a TextCtrl box to hold varibles?

A variable is not something that can be “held”. In fact, thinking in terms of variables isn’t the best model with python.

Rather, you have objects and names:

X = something

Is a name-binding operation. You are binding the name ‘X’ to the object that is bound to the name “something”. If you use a literal:

X = ‘this’

Then you are binding the name to the string object created be the literal.

A TextCtrl is designed to work with string objects–that’s what it takes and provides. Period.

Regexs are expressed as strings, so that should be fine. Or is this a new problem? If so, tell us what you are trying to accomplish.

Chris

···

On Aug 20, 2013, at 12:01 PM, George McCown georgemccown@gmail.com wrote:

Do you have an example?
On Tuesday, August 20, 2013 9:43:16 AM UTC-5, Chris Barker - NOAA Federal wrote:

On Aug 19, 2013, at 5:57 PM, George McCown george...@gmail.com wrote:

I have this widget that works fine for replacing filenames however I’m not getting any results
with adding regex. (self.dirname,f.re.split) I comment out the re.split. I need to know if this is strictly a python

question

Well yes, this is a Python, rather that wxPython–not that you won’t find help here.

But both for you debugging and testing, and to facilitate asking questions, you should pull out the regex stuff into a stand alone function and write some tests against it. Isolating the issue will likely make it much clearer.

Chris

or perhaps this needs another function to work.

-- 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.TxtEnter)


   
    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.BasicRemove.GetValue()) >= 0:


                                print self.AdvanceReplace.GetValue()
                                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,f.re.split(self.AdvanceReplace.GetValue())))

                                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()

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.