"That name can be retrieved from the name member of the file object."
means that you can get the name of the temporary file created like so:
In [4]: import tempfile
In [5]: tf = tempfile.NamedTemporaryFile()
In [6]: tf.name # retrieve the name of the temp file just created
Out[6]: 'c:\\blabla\\locals~1\\temp\\tmptecp3i'
Note: By default the file will be deleted when it is closed. However, if the delete parameter is False, the file is not automatically deleted. See the Python docs on this for more information.
Since you can retrieve the name of each of the 50 temp files you want to create, you can save them, e.g., in a list, before you use them again later (as you say). Just be sure to set the delete value accordingly so that the files don't disappear when you close them (in case you plan to close, and then later reopen them).
I explained how to create temporary filenames in more detail here Best way to generate random file names in Python
Answer from Levon on Stack Overflow"That name can be retrieved from the name member of the file object."
means that you can get the name of the temporary file created like so:
In [4]: import tempfile
In [5]: tf = tempfile.NamedTemporaryFile()
In [6]: tf.name # retrieve the name of the temp file just created
Out[6]: 'c:\\blabla\\locals~1\\temp\\tmptecp3i'
Note: By default the file will be deleted when it is closed. However, if the delete parameter is False, the file is not automatically deleted. See the Python docs on this for more information.
Since you can retrieve the name of each of the 50 temp files you want to create, you can save them, e.g., in a list, before you use them again later (as you say). Just be sure to set the delete value accordingly so that the files don't disappear when you close them (in case you plan to close, and then later reopen them).
I explained how to create temporary filenames in more detail here Best way to generate random file names in Python
You can set file extension and file directory:
tmpfilepath = tempfile.NamedTemporaryFile(dir= 'myDir', suffix='.xlsx')
this will create a file with path:
'...\myDir\tmpstfl1lam.xlsx'
This could be one of two reasons:
Firstly, by default the temporary file is deleted as soon as it is closed. To fix this use:
tf = tempfile.NamedTemporaryFile(delete=False)
and then delete the file manually once you've finished viewing it in the other application.
Alternatively, it could be that because the file is still open in Python Windows won't let you open it using another application.
Edit: to answer some questions from the comments:
As of the docs from 2 when using delete=False the file can be removed by using:
tf.close()
os.unlink(tf.name)
You can also use it with a context manager so that the file will be closed/deleted when it goes out of scope. It will also be cleaned up if the code in the context manager raises.
import tempfile
with tempfile.NamedTemporaryFile() as temp:
temp.write('Some data')
temp.flush()
# do something interesting with temp before it is destroyed