How do I Join all files in os.listdir after re.sub?

I have a multiline TextCtrl I want to output a bunch of files with re.sub.

This box is just for reading os.listdir for testing:

    if

self.filenames:

        path =

self.dirname

dirList=os.listdir(path)

        for

self.filename in dirList:

                f

= self.filename

                print

f ## reads all files

               self.PracInput.SetValue(re.sub(self.PracRemove.GetValue(),self.PracReplace.GetValue(),f))

SetValue is reading only the last file. I understand that. So how can I use something like . join to
re.sub all the files just for reading?

That makes sense, no? You are in a loop and “f” will always only
contain the name of one file.

···

On 24/08/2013 23:04, George McCown
wrote:

      I have a multiline TextCtrl I want to

output a bunch of files with re.sub.

      This box is just for reading os.listdir for

testing:

              if

self.filenames:

      path =

self.dirname

dirList=os.listdir(path)

      for

self.filename in dirList:

      f

= self.filename

      print

f ## reads all files

self.PracInput.SetValue(re.sub(self.PracRemove.GetValue(),self.PracReplace.GetValue(),f))

SetValue is reading only the last file. I understand that. So how can I use
something like . join to
re.sub all the files just for reading?

  path =

self.dirname

dirList=os.listdir(path)

    myFiles = []


  for

self.filename in dirList:

  f

= self.filename
print
f ## reads all files, one
at the time!

        newF =       re.sub(self.PracRemove.GetValue(),

self.PracReplace.GetValue(), f)
myFiles.append(newF)

  self.PracInput.SetValue(myFiles) # note that

this is not in the loop

Werner

Hi,
I guess, the SetValue method of a TextCtrl only accept strings, you
can join the list like: "\n".join(myFiles).
Note, that os.listdir only returns the data for the respective path,
i.e. not some deeper nested directories, but you probably can control
or assume the directory structure, you are accessing..

hth,
   vbr

···

2013/8/25 werner <wbruhin@free.fr>:

On 24/08/2013 23:04, George McCown wrote:

I have a multiline TextCtrl I want to output a bunch of files with re.sub.

This box is just for reading os.listdir for testing:

        if self.filenames:

            path = self.dirname

            dirList=os.listdir(path)

            for self.filename in dirList:

                    f = self.filename

                    print f ## reads all files

self.PracInput.SetValue(re.sub(self.PracRemove.GetValue(),self.PracReplace.GetValue(),f))

SetValue is reading only the last file. I understand that. So how can I
use something like . join to re.sub all the files just for reading?

That makes sense, no? You are in a loop and "f" will always only contain
the name of one file.

path = self.dirname

dirList=os.listdir(path)

myFiles =

for self.filename in dirList:

    f = self.filename
    print f ## reads all files, one at the time!
    newF = re.sub(self.PracRemove.GetValue(), self.PracReplace.GetValue(),
f)
    myFiles.append(newF)

self.PracInput.SetValue(myFiles) # note that this is not in the loop

Werner

--

...

self.PracInput.SetValue(myFiles) # note that this is not in the loop

Werner

--

Hi,
I guess, the SetValue method of a TextCtrl only accept strings, you
can join the list like: "\n".join(myFiles).

Good catch, thanks for correcting me.

Werner

···

On 25/08/2013 14:15, Vlastimil Brom wrote:

2013/8/25 werner <wbruhin@free.fr>:

I implemented:
myFiles.append(newF)
self.PracInput.SetValue(str(“\n”.join(myFiles)))
Works fine now.
Thanks werner,vbr
One more question, is there a way to mark this post as resolved?

···

On Sunday, August 25, 2013 1:10:20 AM UTC-5, werner wrote:

  On 24/08/2013 23:04, George McCown > wrote:
      I have a multiline TextCtrl I want to

output a bunch of files with re.sub.

      This box is just for reading os.listdir for

testing:

              if

self.filenames:

      path =

self.dirname

dirList=os.listdir(path)

      for

self.filename in dirList:

      f

= self.filename

      print

f ## reads all files

self.PracInput.SetValue(re.sub(self.PracRemove.GetValue(),self.PracReplace.GetValue(), f))

SetValue is reading only the last file. I understand that. So how can I use
something like . join to
re.sub all the files just for reading?

That makes sense, no?  You are in a loop and "f" will always only

contain the name of one file.

  path =

self.dirname

dirList=os.listdir(path)

    myFiles = []
  for

self.filename in dirList:

  f

= self.filename
print
f ## reads all files, one
at the time!

        newF = re.sub(self.PracRemove.      GetValue(),

self.PracReplace.GetValue(), f)

      myFiles.append(newF)

self.PracInput.SetValue( myFiles) # note that
this is not in the loop

Werner