0
votes

I am iterating through a list creating a bunch of temporary files at every iteration. At the end of the loop, I would like to append all these temporary files to a main temporary file. How do I append temporary files?

My attempt is below:

    download_file = NamedTemporaryFile(mode='a')
    for i in range(num_files):
        temp_file = NamedTemporaryFile(mode='w+b')
        # do some stuff
        download_file.write(temp_file)
    return download_file
Why not just write to both files...? If you want to do this with a single .write() command, you can abstract it by just creating a wrapper class, e.g. class Tee: def write(*args): self.file_a.write(*args); self.file_b.write(*args) - Mateen Ulhaq