I was wondering if it was possible to get the file path of a temporary file made using the tempfile library. Basically, I'm trying to make a function that intakes some data, and generates a temporary csv file based off of said data. I was wondering if there was a way to get the path of this temporary file?
4 Answers
Use tempfile.NamedTemporaryFile
to create a temporary file with a name, and then use the .name
attribute of the object.
Note that there are platform-specific limitations on how this name can be used. The documentation says:
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).

- 741,623
- 53
- 500
- 612
-
Hmm, what if I wiped the file blank and then re-write it as needed each time? Would it be possible to get a file path to the csv for that? – Keyadun Jun 30 '18 at 00:37
-
1What does rewriting the contents have to do with whether you can get the name? – Barmar Jun 30 '18 at 00:39
-
It doesn't. As an extension to this question, I was wondering if it was possible to change the name of the NamedTemporaryFile that is created. When I send it out "
" is the filename – Keyadun Jul 02 '18 at 23:33 -
1You can use the `prefix` and `suffix` optional arguments to get some control over the name of the file. – Barmar Jul 03 '18 at 03:34
-
Maybe interesting to note that, *on Windows*, `TemporaryFile is NamedTemporaryFile`, as can be seen in the source: https://github.com/python/cpython/blob/3.10/Lib/tempfile.py#L567 – djvg Feb 08 '22 at 15:46
This works just fine to get the full path with the filename
file = tempfile.NamedTemporaryFile()
filename = file.name
The output:
/tmp/tmp_sl4v9ps

- 545
- 7
- 25
-
1As one of the more complete answers explained, doing this can have strange results if you don't close the initial filehandle first and also add the flag to the call to prevent the file from being deleted when the filehandle is closed. As it is, doing anything with the compute name would mean opening and operating on a file that is already open. This is not a good thing to rely on the behavior of. – CryptoFool Sep 20 '22 at 20:04
tempfile.NamedTemporaryFile has a .dir property which will give you want you want.
EDIT: No, it is not .name
, @Barmar, but looking through the source code for tempfile, I don't see a .dir
property either. However, you can use the .name
property in conjunction with os.path
's dirname
method as follows:
with tempfile.NamedTemporaryFile(suffix='.csv', prefix=os.path.basename(__file__)) as tf:
tf_directory = os.path.dirname(tf.name)

- 33,938
- 5
- 80
- 91
-
7
-
-
Where does the question say he wants to extract just the directory part of the pathname? – Barmar Jul 03 '18 at 03:31
-
OP says "I was wondering if there was a way to get the path of this temporary file?" To me, the path is where the given filename is within the filesystem, ergo, the path to the file `/home/hd1/IsAnAutisticTwit` is `/home/hd1/`. os.path.dirname does return this, tf.name returns the absolute path plus the filename delimited by os.sep. – hd1 Jul 03 '18 at 06:14
-
That's not how I interpret it. I think he just means the full pathname of the file, which can be used as a parameter to other functions that deal with files, or sent to other applications so they can access the file. – Barmar Jul 03 '18 at 15:20
-
2This is the first time I have seen somebody calling the parent path to an absolute path the path to the file. I always thought of "the path to a file" as just the absolute path, was I wrong all these years?? – K.-Michael Aye Jan 27 '21 at 08:09
Anyway, if you need the path of the tempfile directory you can use tempfile.gettempdir()

- 305
- 3
- 15