Answers to Best way to generate random file names in Python show how to create temporary files in Python.
I only need to have a temporary file name in my case.
Calling tempfile.NamedTemporaryFile() returns a file handle after actual file creation.
Is there a way to get a filename only? I tried this:
# Trying to get temp file path
tf = tempfile.NamedTemporaryFile()
temp_file_name = tf.name
tf.close()
# Here is my real purpose to get the temp_file_name
f = gzip.open(temp_file_name ,'wb')
...
NamedTemporaryFileguarantees a unique name, (probably) by trying it and retrying if it exists. Getting just a name won't guarantee that you can actually create the file later, you're opening to the race condition of someone else using the same name before you.