Sounds like you have quite a weird script.
It should be something like this (in pseudo code)
open target file for writing.
while (there are files to include into the big file)
{
get next filename to include
open sourcefile for reading.
while (data available in source)
{
Read data from source
write data to target
}
close sourcefile
}
close targetfile
Point being that the target file is not closed untill you are completely finished with it.
If you do have to close the file, you should re-open it using the "a" mode, for appending. Otherwise you will overwrite the target file, erasing everything you wrote into it.