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

Josep M. Fontana josep.m.fontana at gmail.com
Sun Nov 14 17:59:53 CET 2010


Hi Kushal,

First of all, thanks a lot for your help.

<snip>
>
> Your current code adds the marker line to the original files. Is that
> intended?

Well, no, this wasn't intended. The part of the code that did that was
introduced in one of the many attempts to solve the problem. I knew
that this was an unwanted result but since I didn't know how to get
out of the situation I got myself in I decided to send the code as it
was so that people in this list could see more clearly what the
problems with my code were.

> You can open the output file for writing by passing 'w' as the second
> argument to open. You would do this before your directory-walking loop. Then
> when you read the contents of any file, just write to your output file as
> well, along with your marker line.

Is there any reason to open the file before the directory-walking
loop? Below I enclose the code I wrote with your help and Evert's
help. In that code the output file is opened inside the loop and the
script works as expected. Even if it works, this might not be the best
way to do it. Please tell me. I'm learning and I don't just want my
code to work but I want it to be as elegant and efficient as possible.

<snip>
>> ----------------- code--------
>> import os
>> path = '/Volumes/DATA/MyPath'
>> os.chdir(path)
>> file_names = glob.glob('*.txt')
>
> output_stream = open('outputfilename', 'w')
>
>> for subdir, dirs, files in os.walk(path):
>>        for file in files:
>>                f = open(file, 'r')
>>                text = f.readlines()
>
> output_stream.writelines(text)
> output_stream.write('______\n')
>
>>                f.close()
>>                f = open(file, 'a')
>>                f.write('\n\n' + '________________________________' + '\n')
>>                f.close()
>>
>
> output_stream.close()
>
>> ------------

OK, here's what I did:

------------
import os
path = '/Volumes/myPath'
os.chdir(path)
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 statement'
                    data = f.read()
                    with open('/Volumes/myPath2/output.txt', 'a') as
output_file:
                        output_file.write('\n\n<file name="' +
filename + '">\n\n')
                        output_file.write(data)
                        output_file.write('\n\n</file>\n\n')
-----------------

if you notice, the output file is opened on the fourth line counting
from the end, inside the loop. After following Evert's recommendations
and yours, this seemed the most logical place to do it. As I said, it
works like a charm but I don't know if I have committed some crime of
style.

Josep M.


More information about the Tutor mailing list