Search code examples
pythontemporary-filesshutil

Empty/blank result file when using shutil to copy a tempfile


import tempfile
import shutil
tmp_f = tempfile.NamedTemporaryFile(delete=False)
tmp_f.write("hello world")
shutil.copy(tmp_f.name, "/etc/exports")

When I read /etc/exports, it is a completely empty file. what is wrong?


Solution

  • You need to close the file:

    tmp_f.write("hello world")
    tmp_f.close()