how to concisely create a temporary file that is a copy of another file in python

Viewed 24337

I know that it is possible to create a temporary file, and write the data of the file I wish to copy to it. I was just wondering if there was a function like:

create_temporary_copy(file_path)
5 Answers

The following is more concise (OP's ask) than the selected answer. Enjoy!

import tempfile, shutil, os
def create_temporary_copy(path):
  tmp = tempfile.NamedTemporaryFile(delete=True)
  shutil.copy2(path, tmp.name)
  return tmp.name
Related