"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'
I think you're looking for a tempfile.NamedTemporaryFile.
import tempfile
with tempfile.NamedTemporaryFile() as tmp:
print(tmp.name)
tmp.write(...)
But:
Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
If that is a concern for you:
import os, tempfile
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
print(tmp.name)
tmp.write(...)
finally:
tmp.close()
os.unlink(tmp.name)
There is a tempfile module for python, but a simple file creation also does the trick:
new_file = open("path/to/FILE_NAME.ext", "w")
Now you can write to it using the write method:
new_file.write('this is some content')
With the tempfile module this might look like this:
import tempfile
new_file, filename = tempfile.mkstemp()
print(filename)
os.write(new_file, "this is some content")
os.close(new_file)
With mkstemp you are responsible for deleting the file after you are done with it. With other arguments, you can influence the directory and name of the file.
UPDATE
As rightfully pointed out by Emmet Speer, there are security considerations when using mkstemp, as the client code is responsible for closing/cleaning up the created file. A better way to handle it is the following snippet (as taken from the link):
import os
import tempfile
fd, path = tempfile.mkstemp()
try:
with os.fdopen(fd, 'w') as tmp:
# do stuff with temp file
tmp.write('stuff')
finally:
os.remove(path)
The os.fdopen wraps the file descriptor in a Python file object, that closes automatically when the with exits. The call to os.remove deletes the file when no longer needed.
Python has a rich collection of standard libraries to carry out various tasks. In Python, there is a module called tempfile that allows us to create and manipulate temporary files and directories.
We can use tempfile to create temporary files and directories for storing temporary data during the program execution. The module has functions that allow us to create, read, and manipulate temporary files and directories.
The tempfile module includes a function called TemporaryFile() that allows us to create a temporary file for use as temporary storage.
Here the guide to generate and manipulate the temporary files using the tempfile module👇👇
Generate And Manipulate Temporary Files and Directories in Python