[Tutor] Creating one file out of all the files in a directory

Alan Gauld alan.gauld at btinternet.com
Sun Nov 14 18:44:08 CET 2010

"Josep M. Fontana" <josep.m.fontana at gmail.com> wrote

> OK, I ask you (or anybody reading this) the same question I asked
> Kushal: why is it better to open the output file outside the entire
> loop.

Because you only need to open it once and leave it open.
You are opening it each and every time which is wasteful.
It could on some OSDS also lead to problems. And if you
were writing a new file rather than appending it would overwrite
the contents each time which would be bad.

> see the code I came up with after your comments, I open the output
> file inside the loop and the script still works perfectly well.

Consider that more a matter of luck than good judgement.

> Is there any good reason to do it differently from the way I did it?

Yes, see above.

> I came up with this way of doing because I was trying to follow your
> advice of using the 'with' statement and this was the first way that 
> I
> could think of to implement it.

Just put the with statement for the output file outside the loop:

with open('/Volumes/myPath2/output.txt', 'a') as output_file:
  for subdir, dirs, files in os.walk(path):
      for filename in files:
            if filename != '.DS_Store':
                with open(filename, 'r') as f: #see tomboy note 'with 
                    data = f.read()
                    output_file.write('\n\n<file name="' + filename + 

BTW you could use a single write with:

output_file.write( '\n\n<file name=%s>\n\n%s\n\n</file>\n\n" % 
(filename, data) )

But thats largely a matter of personal style.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list