shutil not working in python with a function call
<p>I have a function which is merging certain files from a directory</p>

<pre><code>def merge(path):
    f = open("indexFile","w")
    for path,directory,files in os.walk(path):
        for file in files:
            f1 = open(os.path.join(path,file))
            createCatFile(f1.read())
            print "merging files"
            shutil.copyfileobj(f1, f)
            f1.close()
    f.close()
</code></pre>

<p>Before copying the file object, it is passing the contents of f1 to a function to do some processing. The problem is that the indexFile is created but there is not data in the file. it is an empty file. The <code>createCatFile()</code> function works perfectly as expected. Also, "merging files" is printed number of times the <code>merge()</code> function is called. When I remove the function call to <code>createCatFile()</code> the indexFile is created successfully. </p>

<p>Any help as to what is the problem with this? </p>

<p>The createCatFile function does the following:</p>

<pre><code>def createCatFile(wordtodocstr):
    global offset
    wordInfo = wordtodocstr.split()
    term = wordInfo[0]
    newtermid = wordInfo[1]
    docList = wordInfo[2::2]
    ctfList = [int(number) for number in wordInfo[3::2]]
    docfr = len(docList)
    wordctf = sum(ctfList)
    catFileList = [term, newtermid, str(offset), str(wordctf), str(docfr)]
    catFileJoin = " ".join(catFileList)
    with open(path2+term, "w") as foutterm:
        foutterm.write(catFileJoin)
        foutterm.close()    
    offset+=1
</code></pre>

<p>Thank you.</p>
