Yours in namelessness (was: How to concatenate external files ..?)

Bjorn Pettersen bjorn at roguewave.com
Thu Jun 29 13:23:19 EDT 2000


Cameron Laird wrote:
> 
> In article <slrn8lmb4t.lpi.thantos at brimstone.mecha>,
> Alexander Williams <thantos at gw.total-web.net> wrote:
> >On Thu, 29 Jun 2000 09:59:15 GMT, richard_chamberlain at my-deja.com
> ><richard_chamberlain at my-deja.com> wrote:
> >
> >>               file=open(x,'r')
> >
> >Teach me to post while still mostly asleep here at the office.  I
> >forgot entirely about opening the iterated filename for reading.
> >Bugger.
> >
> >To try and make up for it, how about this wee gem?
> >
> >> # "fls" is the globbed list of filenames
> >> # "outfile" is the file object already opened to write to
> >> flsHandles = map(lambda f: open(f, 'r').read, fls)
> >> for fH in flsHandles:
> >>     outfile.write(fH())
> >
> >Note that you never explicitly give each opened file its own name;
> >instead, you create a list of all the read methods of all the file
> >objects created dynamically from the list of filenames.  (That refered
> >to by "open(FILENAME, 'r').open" is just a function/method reference,
> >it can still be invoked.)
>                         .
>                         .
>                         .
> Well, let's get even more anonymous, and suggest
>   for fH in = map(lambda f: open(f, 'r').read, fls):
>       outfile.write(fH())

You'd be eating up a lot of file descriptors (is that still a real
issue?)  Here's my version :-)

  import glob

  pthstring = r'C:\wxpython\wxPython-2.1.16\docs\wx\wx*.htm'

  outfile = open('temp.txt,'w')
  for name in glob.glob(pthstring):
      outfile.write( open(name).read() )
  outfile.close()

-- bjorn




More information about the Python-list mailing list