I have another method calling this function 3 times to write 3 different files. The first 2 files are copied to the destination folder as expected, the third file is always zero bytes. If I turn off deletion I can see that all 3 temp files are written to successfully. The code reports no errors, but at the end of the process the third file is always empty.
Does anyone know why this is failing 1 out of 3 times?
def write_file(file_destination: str, content: bytes):
with tempfile.NamedTemporaryFile() as fp:
fp.write(content)
shutil.copy(fp.name, file_destination)
The following variation works, however, I would like to understand why the first two files work and the third one does not in the above code.
def write_file(file_destination: str, content: bytes):
with tempfile.NamedTemporaryFile(delete=False) as fp:
fp.write(content)
shutil.copy(fp.name, file_destination)
os.remove(fp.name)