How to concatenate external files ..?

richard_chamberlain at my-deja.com richard_chamberlain at my-deja.com
Thu Jun 29 05:59:15 EDT 2000


Hi Randy,

import sys, string, glob, os

#First I create a list:
fls = glob.glob('C:\\wxpython\\wxPython-2.1.16\\docs\\wx\\wx*.htm')

#Then I guess I should open an output file for writing
outfile = open('temp.txt,'w')

for x in fls:
               file=open(x,'r')
               data=file.read()
               file.close()
               outfile.write(data)

outfile.close()

On your outfile you had 'r' which is read obviously you need to be able
to write to it so 'w' is what you need.

We then iterate over fls to give us each file name and open each one.
We then call the read method against it, which loads in the entire file
and assign that to data. we close the file and write the data to our
outfile. And then on to the next file. Finally we close the outfile.

You have to use read() with some caution because it (trys) to load the
whole file into memory which maybe an issue if it was sizable. In your
case it isn't an issue because they are simple html files.

Richard






In article <slm1odapjev11 at corp.supernews.com>,
  "Randolph MacKenzie" <rmack at eznet.net> wrote:
> I want to combine all the little *.htm files in a directory into one
big
> file.
>
> import sys, string, glob, os
>
> #First I create a list:
> fls = glob.glob('C:\\wxpython\\wxPython-2.1.16\\docs\\wx\\wx*.htm')
>
> #Then I guess I should open an output file for writing
> outfile = open('temp.txt,'r')
>
> Now what  .. how do I iterate over the list and specify that each
file is to
> be read and appended to "outfile" ?
>
> for x in fls:
>     ???
>
> -Randy
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list